Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 59a2adb

Browse files
add 2270
1 parent 2057e6f commit 59a2adb

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|------|----------------|------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------|-------------|-------------
11+
| 2270 |[Number of Ways to Split Array](https://leetcode.com/problems/number-of-ways-to-split-array/)| [Java](../master/src/main/java/com/fishercoder/solutions/_2270.java) || Medium ||
1112
| 2269 |[Find the K-Beauty of a Number](https://leetcode.com/problems/find-the-k-beauty-of-a-number/)| [Java](../master/src/main/java/com/fishercoder/solutions/_2269.java) || Easy ||
1213
| 2264 |[Largest 3-Same-Digit Number in String](https://leetcode.com/problems/largest-3-same-digit-number-in-string/)| [Java](../master/src/main/java/com/fishercoder/solutions/_2264.java) || Easy ||
1314
| 2260 |[Minimum Consecutive Cards to Pick Up](https://leetcode.com/problems/minimum-consecutive-cards-to-pick-up/)| [Java](../master/src/main/java/com/fishercoder/solutions/_2260.java) || Medium ||
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _2270 {
4+
public static class Solution1 {
5+
public int waysToSplitArray(int[] nums) {
6+
long[] presum = new long[nums.length];
7+
for (int i = 0; i < nums.length; i++) {
8+
if (i == 0) {
9+
presum[i] = nums[i];
10+
} else {
11+
presum[i] = presum[i - 1] + nums[i];
12+
}
13+
}
14+
int ways = 0;
15+
long firstHalf = presum[0];
16+
long secondHalf = presum[nums.length - 1] - presum[0];
17+
for (int i = 0; i < nums.length - 1; ) {
18+
if (firstHalf >= secondHalf) {
19+
ways++;
20+
}
21+
i++;
22+
if (i < nums.length - 1) {
23+
firstHalf = presum[i];
24+
secondHalf = presum[nums.length - 1] - presum[i];
25+
}
26+
}
27+
return ways;
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)