problem
solution
1 | class Solution { |
- reduce space
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15class Solution {
public:
int pivotIndex(vector<int>& nums) {
int right_sum = 0, left_sum= 0;
for(int n:nums) right_sum +=n;
int n = nums.size();
for(int i=0;i<n;++i)
{
right_sum-=nums[i];
if(left_sum == right_sum) return i;
left_sum+=nums[i];
}
return -1;
}
};
analysis
- time complexity
O(n)
- space complexity
O(n)
->O(1)