Skip to content

Commit

Permalink
Section3-6. 최대 길이 연속부분수열
Browse files Browse the repository at this point in the history
  • Loading branch information
gyuseon25 committed Jan 2, 2025
1 parent fe7d064 commit e3e431f
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions Section03/최대_길이_연속부분수열.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package 인프런.Section03;

import java.util.Scanner;

public class 최대_길이_연속부분수열 {
public static int solution(int[] arr, int k) {

int answer = 0, count = 0, lt = 0;

for(int rt = 0; rt < arr.length; rt++) {
if(arr[rt] == 0) count++;
while(count > k) {
if(arr[lt] == 0) count--;
lt++;
}
answer = Math.max(answer, rt-lt+1);
}

return answer;
}

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int k = scanner.nextInt();

int arr[] = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
}

System.out.print(solution(arr, k));
}
}

0 comments on commit e3e431f

Please sign in to comment.