230519 연속된 부분 수열의 합

Jongleee·2023년 5월 19일
0

TIL

목록 보기
263/737
public int[] solution(int[] sequence, int k) {
    int left = 0;
    int right = 0;
    int minLength = Integer.MAX_VALUE;
    int currentSum = 0;
    int[] answer = new int[2];

    while (right < sequence.length && left <= right) {
        if (left == right) {
            currentSum = sequence[left];
        }

        if (currentSum == k) {
            int currentLength = right - left + 1;

            if (currentLength < minLength) {
                minLength = currentLength;
                answer[0] = left;
                answer[1] = right;
            }

            currentSum -= sequence[left];

            currentSum = checkCondition(sequence, right, currentSum);

            if (left == right) {
                break;
            }

            left++;
            right++;
        } else if (currentSum > k) {
            currentSum -= sequence[left];
            left++;
        } else {
            currentSum = checkCondition(sequence, right, currentSum);
            right++;
        }
    }

    return answer;
}

private int checkCondition(int[] sequence, int right, int currentSum) {
    if (right + 1 < sequence.length) {
        currentSum += sequence[right + 1];
    }
    return currentSum;
}

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

0개의 댓글