240527 겹치는 건 싫어

Jongleee·2024년 5월 27일
0

TIL

목록 보기
583/737
public static void main(String[] args) throws IOException {
	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	StringTokenizer st = new StringTokenizer(br.readLine());
	int n = Integer.parseInt(st.nextToken());
	int k = Integer.parseInt(st.nextToken());

	int[] sequence = new int[n];
	st = new StringTokenizer(br.readLine());
	for (int i = 0; i < n; i++) {
		sequence[i] = Integer.parseInt(st.nextToken());
	}

	int[] cnt = new int[100001];
	int left = 0;
	int right = 0;
	int max = 0;
	while (right < n) {
		if (cnt[sequence[right]] < k) {
			cnt[sequence[right]]++;
			right++;
			max = Math.max(max, right - left);
		} else {
			cnt[sequence[left]]--;
			left++;
		}
	}
	System.out.println(max);
}

출처:https://www.acmicpc.net/problem/20922

0개의 댓글