2270. Number of Ways to Split Array

problem

solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public:
int waysToSplitArray(vector<int>& nums) {
int count = 0, n=nums.size();
long long cur = 0, total = 0;
for(int n:nums) total+=n;
for(int i=0;i<n-1;++i){
cur+=nums[i];
total-=nums[i];
if(cur>=total) count++;
}
return count;
}
};

ananlysis

  • time complexity O(n)
  • space complexity O(1)