forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path_2270.java
30 lines (29 loc) · 962 Bytes
/
_2270.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.fishercoder.solutions;
public class _2270 {
public static class Solution1 {
public int waysToSplitArray(int[] nums) {
long[] presum = new long[nums.length];
for (int i = 0; i < nums.length; i++) {
if (i == 0) {
presum[i] = nums[i];
} else {
presum[i] = presum[i - 1] + nums[i];
}
}
int ways = 0;
long firstHalf = presum[0];
long secondHalf = presum[nums.length - 1] - presum[0];
for (int i = 0; i < nums.length - 1; ) {
if (firstHalf >= secondHalf) {
ways++;
}
i++;
if (i < nums.length - 1) {
firstHalf = presum[i];
secondHalf = presum[nums.length - 1] - presum[i];
}
}
return ways;
}
}
}