内卷地狱

2270. Number of Ways to Split Array

Edit Me

Problem

2270. Number of Ways to Split Array

Approach

2 <= nums.length <= 10^5, so we can directly take the first element. The initial state has the pointer at index 0, about to move to index 1. A single for loop is all we need.

The second method is the key one — taken from the editorial.

Code

class Solution:
    def waysToSplitArray(self, nums: List[int]) -> int:
        temp_sum = nums[0]
        total_sum = sum(nums) - temp_sum
        ans = 0
        for i in range(1, len(nums)):
            if temp_sum >= total_sum:
                ans += 1
            temp_sum += nums[i]
            total_sum -= nums[i]
        return ans
t = (sum(nums) + 1) // 2
return sum(s >= t for s in accumulate(nums[:-1]))

贡献者


这篇文章有帮助吗?

最近更新

Involution Hell© 2026 byCommunityunderCC BY-NC-SA 4.0CCBYNCSA