오늘 풀어본 문제는 ⭐후보 추천하기라는 문제이다.
오랜만에 Map을 쓰니 좀 헷갈렸다. Map 복습용 문제!
class Info implements Comparable<Info> {
int num;
int great;
int order;
public Info(int num, int great, int order) {
this.num=num;
this.great=great;
this.order=order;
}
@Override
public int compareTo(Info o){
if(this.great == o.great)
return this.order - o.order;
return this.great - o.great;
}
}
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
int N = Integer.parseInt(br.readLine());
int greatCnt = Integer.parseInt(br.readLine());
//번호, 자료구조
Map<Integer, Info> list = new HashMap<>();
st = new StringTokenizer(br.readLine());
int order = 0;
while (greatCnt --> 0) {
int now = Integer.parseInt(st.nextToken());
order++;
if(list.containsKey(now))
{
list.get(now).great++;
continue;
}
if(list.size() >= N && !list.containsKey(now)) {
Info out = findOut(list);
list.remove(out.num);
}
list.put(now, new Info(now,1, order));
}
List<Integer> keys = new ArrayList<>(list.keySet());
Collections.sort(keys);
StringBuilder sb = new StringBuilder();
for (int key : keys) {
sb.append(key).append(" ");
}
System.out.println(sb);
}
static Info findOut(Map<Integer, Info> list){
PriorityQueue<Info> queue = new PriorityQueue<>();
for(Info value : list.values()){
queue.add(value);
}
return queue.poll();
}
}
