#Why does my code have such terrible time and space efficiency??
12 messages · Page 1 of 1 (latest)
you could follow a better approach
instead get the sum of the whole list once, then have a variable left_sum = 0 ..eg and loop through range(0, len(sums)) and add to the left sum right_sum += nums[i] calculate right sum right_sum = total_sum - left_sum, if they are equal return the index otherwise continue
you can find better explanation in here
https://leetcode.com/problems/find-pivot-index/editorial/
So you would have O(n) runtime and O(1) space
O(1) since you don't count the size of args passed in
Your runtime is roughly n^2, accounting for the for loop and the in check
You can further optimize to make it O(n) by sacrificing a bit of space and using a dictionary
fyi dictionary is O(1) lookup
hashmap moment