Skip to content

Commit 9d99df4

Browse files
committed
Sync LeetCode submission Runtime - 90 ms (22.05%), Memory - 33 MB (29.18%)
1 parent f1976ac commit 9d99df4

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code>.</p>
2+
3+
<p><code>nums</code> contains a <strong>valid split</strong> at index <code>i</code> if the following are true:</p>
4+
5+
<ul>
6+
<li>The sum of the first <code>i + 1</code> elements is <strong>greater than or equal to</strong> the sum of the last <code>n - i - 1</code> elements.</li>
7+
<li>There is <strong>at least one</strong> element to the right of <code>i</code>. That is, <code>0 &lt;= i &lt; n - 1</code>.</li>
8+
</ul>
9+
10+
<p>Return <em>the number of <strong>valid splits</strong> in</em> <code>nums</code>.</p>
11+
12+
<p>&nbsp;</p>
13+
<p><strong class="example">Example 1:</strong></p>
14+
15+
<pre>
16+
<strong>Input:</strong> nums = [10,4,-8,7]
17+
<strong>Output:</strong> 2
18+
<strong>Explanation:</strong>
19+
There are three ways of splitting nums into two non-empty parts:
20+
- Split nums at index 0. Then, the first part is [10], and its sum is 10. The second part is [4,-8,7], and its sum is 3. Since 10 &gt;= 3, i = 0 is a valid split.
21+
- Split nums at index 1. Then, the first part is [10,4], and its sum is 14. The second part is [-8,7], and its sum is -1. Since 14 &gt;= -1, i = 1 is a valid split.
22+
- Split nums at index 2. Then, the first part is [10,4,-8], and its sum is 6. The second part is [7], and its sum is 7. Since 6 &lt; 7, i = 2 is not a valid split.
23+
Thus, the number of valid splits in nums is 2.
24+
</pre>
25+
26+
<p><strong class="example">Example 2:</strong></p>
27+
28+
<pre>
29+
<strong>Input:</strong> nums = [2,3,1,0]
30+
<strong>Output:</strong> 2
31+
<strong>Explanation:</strong>
32+
There are two valid splits in nums:
33+
- Split nums at index 1. Then, the first part is [2,3], and its sum is 5. The second part is [1,0], and its sum is 1. Since 5 &gt;= 1, i = 1 is a valid split.
34+
- Split nums at index 2. Then, the first part is [2,3,1], and its sum is 6. The second part is [0], and its sum is 0. Since 6 &gt;= 0, i = 2 is a valid split.
35+
</pre>
36+
37+
<p>&nbsp;</p>
38+
<p><strong>Constraints:</strong></p>
39+
40+
<ul>
41+
<li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
42+
<li><code>-10<sup>5</sup> &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
43+
</ul>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Approach 1: Prefix Sum Array
2+
3+
# Time: O(n)
4+
# Space: O(n)
5+
6+
class Solution:
7+
def waysToSplitArray(self, nums: List[int]) -> int:
8+
n = len(nums)
9+
prefix_sum = [0] * n
10+
prefix_sum[0] = nums[0]
11+
12+
for i in range(1, n):
13+
prefix_sum[i] = prefix_sum[i - 1] + nums[i]
14+
15+
# Check each possible split position
16+
count = sum(1 for i in range(n - 1) if prefix_sum[i] >= prefix_sum[-1] - prefix_sum[i])
17+
18+
return count
19+

0 commit comments

Comments
 (0)