ffinn92 / Keep-at-solve-it

꾸준히 알고리즘 풀기 위한 스터디 저장소입니다.
2 stars 3 forks source link

[220706][BC][인프런](3-6) 최대 길이 연속부분수열 #81

Closed honeySleepr closed 2 years ago

honeySleepr commented 2 years ago

📌 문제

⭐️ 아이디어

🤔 고민한 내용

💪 새롭게 배운 내용

🆘 이해가 어려운 내용

❌ 해결하지 못한 이유

✅ 본인 풀이

image

// 최대 길이 연속부분수열
public class P0306 {

    private static int solution(int n, int k, int[] arr) {
        int l = 0;
        int answer = 0;
        int count = 0;

        for (int r = 0; r < arr.length; r++) {

            if (arr[r] == 0) {
                count++;
            }
            while (count > k) {
                if (arr[l] == 0) {
                    count--;
                }
                l++;
            }
            answer = Math.max(answer, r - l + 1);
        }

        return answer;
    }

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] split = br.readLine().split("\\s");
        int n = Integer.parseInt(split[0]);
        int k = Integer.parseInt(split[1]);
        String[] split2 = br.readLine().split("\\s");
        int[] arr = new int[split2.length];

        for (int i = 0; i < split2.length; i++) {
            arr[i] = Integer.parseInt(split2[i]);
        }

        int answer = solution(n, k, arr);
        System.out.println(answer);
    }
}

참고한 자료