[백준] 21939 문제 추천 시스템 Version 1 - Java
기준에 맞춰 정렬해 주기 위해 Comparable 인터페이스를 구현한 클래스를 하나 만들고, 클래스를 바탕으로 트리셋에 넣어 주며 구현하면 풀 수 있는 문제이다.
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
TreeSet<Problem> ts = new TreeSet<>();
HashMap<Integer, Integer> hm = new HashMap<>();
int N = Integer.parseInt(br.readLine());
StringTokenizer st;
for(int i=0; i<N; i++){
st = new StringTokenizer(br.readLine());
int num = Integer.parseInt(st.nextToken());
int difficulty = Integer.parseInt(st.nextToken());
hm.put(num, difficulty);
ts.add(new Problem(num, difficulty));
}
int M = Integer.parseInt(br.readLine());
for(int i=0; i<M; i++){
st = new StringTokenizer(br.readLine());
String command = st.nextToken();
if(command.equals("add")){
int num = Integer.parseInt(st.nextToken());
int difficulty = Integer.parseInt(st.nextToken());
ts.add(new Problem(num, difficulty));// 중복 검사 해야되지 않음?
hm.put(num, difficulty);
}else if(command.equals("recommend")){
int num = Integer.parseInt(st.nextToken());
if(num == 1){
sb.append(ts.last().num + "\n");
}else if(num == -1){
sb.append(ts.first().num + "\n");
}
}else if(command.equals("solved")){
int num = Integer.parseInt(st.nextToken());
int difficulty = hm.get(num);
ts.remove(new Problem(num , difficulty));
}
}
System.out.println(sb);
}
static class Problem implements Comparable<Problem> {
int num = 0;
int difficulty = 0;
Problem(int a, int b) {
num = a;
difficulty = b;
}
public int compareTo(Problem o1) {
if (difficulty > o1.difficulty) {
return 1;
} else if (difficulty < o1.difficulty) {
return -1;
} else {
if(num > o1.num){
return 1;
}else if(num < o1.num){
return -1;
}else{
return 0;
}
}
}
} // end of class Problem
}
트리셋에 넣어줄 때, 해시맵에도 같이 넣어줘서 추후 add에서 문제를 넣어줄 때 문제의 난이도를 가져올 수 있도록 했다.
반응형
'Algorithm > Baekjoon' 카테고리의 다른 글
[백준] 12757 전설의 JBNU - Java (0) | 2022.04.23 |
---|---|
[백준] 21944 문제 추천 시스템 Version 2 - Java (0) | 2022.04.23 |
[백준] 1351 무한수열 - Java (0) | 2022.04.23 |
[백준] 2002 추월 - Java (0) | 2022.04.23 |
[백준] 19583 싸이버개강총회 - Java (0) | 2022.04.23 |
댓글
이 글 공유하기
다른 글
-
[백준] 12757 전설의 JBNU - Java
[백준] 12757 전설의 JBNU - Java
2022.04.23 -
[백준] 21944 문제 추천 시스템 Version 2 - Java
[백준] 21944 문제 추천 시스템 Version 2 - Java
2022.04.23 -
[백준] 1351 무한수열 - Java
[백준] 1351 무한수열 - Java
2022.04.23 -
[백준] 2002 추월 - Java
[백준] 2002 추월 - Java
2022.04.23