forked from tony9402/baekjoon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
68 lines (58 loc) · 1.9 KB
/
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//Authored by : suin8
//Co-authored by : tony9402
//Link : http://boj.kr/a390775f4af44af3af4239329e91fdcb
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;
}
}
}