문제에 대한 자세한 정보는 백준 | 11652번 : 카드에서 확인할 수 있다.
적혀있는 수의 범위가 -2^62~2^62이므로 저장할 때는 long type을 사용해야 한다.
수 정렬하기 3 문제에서 Map을 사용해 풀었던 것처럼 풀이했다.
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int N = Integer.parseInt(br.readLine());
Map<Long, Integer> map = new HashMap<>();
for (int i = 0; i < N; i++) {
long temp = Long.parseLong(br.readLine());
if (map.containsKey(temp))
map.put(temp, map.get(temp) + 1);
else
map.put(temp, 1);
}
Set<Long> keys = map.keySet();
int count = 0;
long num = 0;
for(long key : keys) {
if(count < map.get(key)) {
count = map.get(key);
num = key;
} else if(count == map.get(key)) {
num = Math.min(num, key);
count = map.get(num);
}
}
bw.write(String.valueOf(num));
br.close();
bw.close();
}
}
메모리 : 41208KB
시간 : 388ms
가장 큰 value를 가려내고, value가 같으면 가장 작은 key값을 가려내는 for문만 잘 작성하면 크게 어려운 문제는 아니었다.
문제와는 관련없는 이야기지만 미리보기에서는 sub, sup 태그가 잘 작동하는데 막상 출간 후 확인하면 적용이 안 되어있다..