공부기록/알고리즘
[알고리즘]P1713. 후보 추천하기
쇼파죠하
2024. 2. 14. 17:56
package day25.P1713_후보추천하기;
import java.util.LinkedList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt(); // 사진틀의 개수
int K = sc.nextInt(); // 학생들의 총 추천 횟수
Candidate[] candidates = new Candidate[101]; // 후보자 번호가 100번까지여서 걍 인덱스 1부터 쓸라고 101개만듦.
LinkedList<Integer> list = new LinkedList<>();
for (int i = 0; i < K; i++) {
int num = sc.nextInt();
if (candidates[num] == null) {
if (list.size() == N) { // 사진틀이 가득 찼을 때
int removed = list.removeFirst(); // 가장 오래된 학생 제거
candidates[removed] = null;
}
candidates[num] = new Candidate(num, 0, i, true); // 새 학생 추가
list.addLast(num);
} else {
candidates[num].recommend++; // 이미 존재하는 학생의 추천 횟수 증가
}
// 사진틀의 순서를 정하는 부분
list.sort((o1, o2) -> {
if (candidates[o1].recommend == candidates[o2].recommend) {
return candidates[o1].time - candidates[o2].time;
}
return candidates[o1].recommend - candidates[o2].recommend;
});
}
// 정렬된 사진틀에 있는 학생들을 출력
for (int student : list) {
System.out.print(student + " ");
}
}
static class Candidate {
int num; // 학생 번호
int recommend; // 추천 횟수
int time; // 사진틀에 들어온 시간
boolean exist; // 사진틀에 존재하는지 여부
public Candidate(int num, int recommend, int time, boolean exist) {
this.num = num;
this.recommend = recommend;
this.time = time;
this.exist = exist;
}
}
}
1713 후보 추천하기
근데 마지막 출력 부분에 오름차순으로 출력되도록 만들어야 하는데
난 계속 6 7 2 로 출력됨
이 부분 수정해서 제출해야할듯
반응형