File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed
Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change 1+ ---
2+ title : 2270. Number of Ways to Split Array.md
3+ date : ' 2025/1/14-9:31'
4+ tags :
5+ - - Python
6+ - - Answer
7+ abbrlink : c25bb550
8+ ---
9+
10+ # QUESTION:
11+
12+ [ 2270. Number of Ways to Split Array.md] ( https://leetcode.cn/problems/number-of-ways-to-split-array/description/ )
13+
14+ # My Think:
15+ ` 2 <= nums.length <= 105 ` , 因此我们可以直接获取到第一个数字, 初始状态就在指针于index0, 正要往index1走的时候.
16+ 然后只需要一次For循环就可以搞定
17+
18+
19+ 重点是第二个方法, 来自题解.
20+
21+ # Code:
22+
23+ ``` python
24+ class Solution :
25+ def waysToSplitArray (self , nums : List[int ]) -> int :
26+ temp_sum = nums[0 ]
27+ total_sum = sum (nums) - temp_sum
28+ ans = 0
29+ for i in range (1 , len (nums)):
30+ if temp_sum >= total_sum:
31+ ans += 1
32+ temp_sum += nums[i]
33+ total_sum -= nums[i]
34+ return ans
35+ ```
36+
37+ ``` python
38+ t = (sum (nums) + 1 ) // 2
39+ return sum (s >= t for s in accumulate(nums[:- 1 ]))
40+ ```
You can’t perform that action at this time.
0 commit comments