https://school.programmers.co.kr/learn/courses/30/lessons/49995
class Solution {
public int solution(int[] cookie) {
int N = cookie.length;
int[] preSum = new int[N];
//구간합 O(1)을 위해 누적합 배열 구하기
int sum = 0;
for (int i = 0; i < N; i++) {
sum += cookie[i];
preSum[i] = sum;
}
int result = 0;
for (int i = 0; i < N-1; i++) {
for (int j = i+1; j < N; j++) {
sum = preSum[j] - (i-1 < 0 ? 0 : preSum[i-1]);
if (sum / 2 <= result) continue;
//i ~ M, M+1 ~ j 범위일 때, M 구하기
int lo = i;
int hi = j;
int before = -1;
while(lo < hi) {
int mid = (lo + hi) / 2;
if (mid == before) break;
before = mid;
//i ~ mid 구간합
int sum1 = preSum[mid] - (i-1 < 0 ? 0 : preSum[i-1]);
int sum2 = preSum[j] - preSum[mid];
if (sum1 > sum2) hi = mid;
else if (sum1 < sum2) lo = mid;
else {
result = Math.max(result, sum1);
break;
}
}
}
}
return result;
}
}