💻 문제 출처 : 백준_문제 추천 시스템 Version 1
import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int questionCount = Integer.parseInt(br.readLine());
TreeSet<Integer> questionLevel = new TreeSet<>();
Map<Integer, TreeSet<Integer>> questions = new HashMap<>();
for(int i = 0; i < questionCount; i++) {
String[] strArray = br.readLine().split(" ");
int questionNum = Integer.parseInt(strArray[0]);
int level = Integer.parseInt(strArray[1]);
questionLevel.add(level);
questions.computeIfAbsent(level, k -> new TreeSet<>()).add(questionNum);
}
int commandCount = Integer.parseInt(br.readLine());
for(int i = 0; i < commandCount; i++) {
String[] strArray = br.readLine().split(" ");
String command = strArray[0];
switch(command) {
case "add": int level = Integer.parseInt(strArray[2]);
int questionNum = Integer.parseInt(strArray[1]);
questionLevel.add(level);
questions.computeIfAbsent(level, k -> new TreeSet<>()).add(questionNum);
break;
case "recommend": if(Integer.parseInt(strArray[1]) == 1) {
int questionRecommend = questionLevel.last();
System.out.println(questions.get(questionRecommend).last());
} else {
int questionRecommend = questionLevel.first();
System.out.println(questions.get(questionRecommend).first());
}
break;
case "solved": int solvedNum = 0;
int solvedQuestionNum = Integer.parseInt(strArray[1]);
for(Map.Entry<Integer, TreeSet<Integer>> entry : questions.entrySet()) {
if(entry.getValue().floor(solvedQuestionNum) == null) {
continue;
}
int closeQuestionNum = entry.getValue().floor(solvedQuestionNum);
if(closeQuestionNum == solvedQuestionNum) {
solvedNum = entry.getKey();
break;
}
}
questions.get(solvedNum).remove(solvedQuestionNum);
if(questions.get(solvedNum).isEmpty()) questionLevel.remove(solvedNum);
break;
}
}
}
}
📌 문제 풀이 설명
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.TreeSet;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
StringBuffer sb = new StringBuffer();
int[] difficulty = new int[100001];
TreeSet<Integer> ts = new TreeSet<>((o1, o2) -> {
if (difficulty[o2] == difficulty[o1]) return o2-o1;
else return difficulty[o2] - difficulty[o1];
});
for (int i = 0; i<N; i++) {
StringTokenizer st = new StringTokenizer(br.readLine()," ");
int P = Integer.parseInt(st.nextToken());
int L = Integer.parseInt(st.nextToken());
difficulty[P] = L;
ts.add(P);
}
int M = Integer.parseInt(br.readLine());
for (int i = 0; i<M; i++) {
StringTokenizer st = new StringTokenizer(br.readLine()," ");
switch(st.nextToken()) {
case("add") :
int P = Integer.parseInt(st.nextToken());
int L = Integer.parseInt(st.nextToken());
difficulty[P] = L;
ts.add(P);
break;
case ("solved") :
ts.remove(Integer.parseInt(st.nextToken()));
break;
case ("recommend") :
int x = Integer.parseInt(st.nextToken());
if (x == 1) {
sb.append(ts.first()).append("\n");
}
else {
sb.append(ts.last()).append("\n");
}
break;
}
}
System.out.println(sb);
}
}
📌 문제 풀이 설명