SOLUTION INFO
C++ · main.cpp
- 작성자
- suin8
- 공동 작성자
- tony9402
#include <bits/stdc++.h>
using namespace std;
int num[200010], cnt[100010];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, K; cin >> N >> K;
for(int i = 1;i <= N;i++)
cin >> num[i];
// start와 end사이에 겹치는 수가 K개 이하일 때는 end를 늘리며
// 최대 길이를 늘리고
// K개 초과일 때는 begin를 늘리며 겹치는 수가 빠질때까지 begin을 증가
int ans = 0;
for(int begin = 1, end = 1; begin <= N; begin ++) {
while(end <= N && cnt[num[end]] < K) ++cnt[num[end++]];
ans = max(ans, end - begin);
cnt[num[begin]]--;
}
cout << ans;
return 0;
}
SOLUTION DESCRIPTION
풀이 설명
등록된 풀이 설명이 없습니다.
SOLUTION INFO
Java · Main.java
- 작성자
- suin8
- 공동 작성자
- tony9402
import java.util.*;
import java.io.*;
public class Main {
static int[] num = new int[200010];
static int[] count = new int[100010];
public static void main(String[] args) {
FastReader rd = new FastReader();
int N = rd.nextInt();
int K = rd.nextInt();
for(int i = 1;i <= N;i++)
num[i] = rd.nextInt();
// 투포인터로 풀이
// start와 end사이에 겹치는 수가 K개 이하일 때는 end증가
// K개 초과일 때는 start를 늘려가며 겹치는 수가 빠질때까지 begin증가
int max = 0;
for(int begin = 1, end = 1; begin <= N; begin ++) {
while(end <= N && count[num[end]] < K) {
count[num[end]] ++;
end ++;
}
max = Math.max(end - begin, max);
count[num[begin]] --;
}
System.out.println(max);
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while(st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
String nextLine() {
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
SOLUTION DESCRIPTION
풀이 설명
등록된 풀이 설명이 없습니다.