import java.util.*;
class Main {
public ArrayList<Integer> solution(int n, int k, int[] arr) {
ArrayList<Integer> answer = new ArrayList<>();
HashMap<Integer,Integer> map = new HashMap<>();
int rt =0;
for(int lt=0; lt<=n-k; lt++) {
while(rt-lt < k) { map.put(arr[rt], map.getOrDefault(arr[rt], 0)+1);rt++;}
answer.add(map.size());
//arr[lt]값 빼주기.
if(map.get(arr[lt]) == 1) {map.remove(arr[lt]);}
else map.put(arr[lt], map.get(arr[lt])-1);
}
return answer;
}
public static void main(String[] args) {
Main T = new Main();
Scanner kb = new Scanner(System.in);
int n = kb.nextInt();
int k = kb.nextInt();
int[] arr = new int[n];
for(int i = 0; i < n; i++) arr[i] = kb.nextInt();
for(int x : T.solution(n, k, arr)) {
System.out.print(x + " ");
}
}
}
import java.util.*;
class Main {
public ArrayList<Integer> solution(int n, int k, int[] arr) {
int answer = 0;
ArrayList<Integer> answer = new ArrayList<>();
HashMap<Integer, Integer> HM = new HashMap<>();
//미리 처음값 셋팅
for(int i=0; i<k-1; i++) {
HM.put(arr[i], HM.getOrDefault(arr[i], 0)+1);
}
int lt = 0;
for(int rt = k-1; rt<n; rt++) {
//rt값 추가.
HM.put(arr[rt], HM.getOrDefault(arr[rt], 0)+1);
answer.add(HM.size());
//lt가쫓아감. (value가 0이면 remove)
HM.put(arr[lt],get(arr[lt])-1);
if(HM.get(arr[lt]) == 0) HM.remove(arr[lt]);
lt++;
}
return answer;
}
public static void main(String[] args) {
Main T = new Main();
Scanner kb = new Scanner(System.in);
int n = kb.nextInt();
int k = kb.nextInt();
int[] arr = new int[n];
for(int i = 0; i < n; i++) arr[i] = kb.nextInt();
for(int x : T.solution(n, k, arr)) {
System.out.print(x + " ");
}
}
}
초기값을 설정해줘서 푸는방법을 숙지하자!