main idea: given total sum, use total-psum[left] - pivot == psum[left]
class Solution:
def pivotIndex(self, nums: List[int]) -> int:
# time O(N) space (1)
# step1: get total sum
total = sum(nums)
psum = 0
# step2: iterate through nums, get psum
for i,num in enumerate(nums):
# step3: check if current number is pivot number ==> total -psum + current num == psum
if total - psum - num == psum:
return i
psum += num
# step4 if none meet pivot standard return -1
return -1