230227 쿠키 구입

Jongleee·2023년 2월 27일
0

TIL

목록 보기
192/576
public static 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 < secondSum)
                answer = Math.max(answer, secondSum);
            else if (firstSum <= secondSum && firstIndex > 0)
                firstSum += cookie[--firstIndex];
            else if (firstSum > secondSum && secondIndex < cookie.length - 1)
                secondSum += cookie[++secondIndex];
            else
                break;

        }
    }
    return answer;
}

0개의 댓글