Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1d07d14

Browse files
alexpantyukhinPanquesito7
andauthored
feat: add Number of Ways to Split Array LeetCode (#1165)
Co-authored-by: David Leal <halfpacho@gmail.com>
1 parent 8992d26 commit 1d07d14

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

‎leetcode/DIRECTORY.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,5 @@
109109
| 1752 | [Check if Array Is Sorted and Rotated](https://leetcode.com/problems/check-if-array-is-sorted-and-rotated/) | [C](./src/1752.c) | Easy |
110110
| 2024 | [Maximize the Confusion of an Exam](https://leetcode.com/problems/maximize-the-confusion-of-an-exam/) | [C](./src/2024.c) | Medium |
111111
| 2130 | [Maximum Twin Sum of a Linked List](https://leetcode.com/problems/maximum-twin-sum-of-a-linked-list/) | [C](./src/2130.c) | Medium |
112+
| 2270 | [Number of Ways to Split Array](https://leetcode.com/problems/number-of-ways-to-split-array/) | [C](./src/2270.c) | Medium |
112113
| 2304 | [Minimum Path Cost in a Grid](https://leetcode.com/problems/minimum-path-cost-in-a-grid/) | [C](./src/2304.c) | Medium |

‎leetcode/src/2270.c‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Prefix sum.
2+
// Collect sum fromleft part and compare it with left sum.
3+
// Runtime: O(n)
4+
// Space: O(1)
5+
int waysToSplitArray(int* nums, int numsSize){
6+
long sumNums = 0;
7+
for (int i = 0; i < numsSize; i++){
8+
sumNums += nums[i];
9+
}
10+
11+
long prefixSum = 0;
12+
int result = 0;
13+
for (int i = 0; i < numsSize - 1; i++){
14+
prefixSum += nums[i];
15+
if (prefixSum >= sumNums - prefixSum){
16+
result += 1;
17+
}
18+
}
19+
20+
return result;
21+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /