231205 쿠키 구입

Jongleee·2023년 12월 5일
0

TIL

목록 보기
434/576
public int solution(int[] cookie) {
	int answer = 0;
	for (int i = 0; i < cookie.length - 1; i++) {
		int firstIndex = i;
		int secondIndex = i + 1;
		int firstSum = cookie[firstIndex];
		int secondSum = cookie[secondIndex];

		while (true) {
			if (firstSum == secondSum) {
				answer = Math.max(answer, secondSum);
			}

			if (firstSum <= secondSum && firstIndex > 0) {
				firstSum += cookie[--firstIndex];
			} else if (firstSum > secondSum && secondIndex < cookie.length - 1) {
				secondSum += cookie[++secondIndex];
			} else {
				break;
			}
		}
	}
	return answer;
}

출처:https://school.programmers.co.kr/learn/courses/30/lessons/49995

0개의 댓글