Skip to content

2270_NumberofWaystoSplitArray

a920604a edited this page Apr 14, 2023 · 1 revision

title: 2270. Number of Ways to Split Array categories: leetcode comments: false

solution

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)
Clone this wiki locally