import java.util.*;
class Solution {
public Integer[] solution(int[] answers) {
int[] answer = {};
int[] one = {1,2,3,4,5};
int[] two = {2,1,2,3,2,4,2,5};
int[] three = {3,3,1,1,2,2,4,4,5,5};
// System.out.println(1%one.length);
Queue<Node> q = new PriorityQueue<>();
Node fir = new Node(1,0);
Node sec = new Node(2,0);
Node thd = new Node(3,0);
if(one[0] == answers[0]){
fir.cnt++;
}
if(two[0] == answers[0]){
sec.cnt++;
}
if(three[0] == answers[0]){
thd.cnt++;
}
for(int i = 1; i < answers.length; i++){
if(one[i%one.length] == answers[i]){
fir.cnt++;
}
if(two[i%two.length] == answers[i]){
sec.cnt++;
}
if(three[i%three.length] == answers[i]){
thd.cnt++;
}
}
q.add(fir);
q.add(sec);
q.add(thd);
Node maxNode = q.poll();
int max = maxNode.cnt;
List<Integer> arr = new ArrayList<>();
arr.add(maxNode.pos);
while(!q.isEmpty()){
Node now = q.poll();
if(now.cnt == max){
arr.add(now.pos);
}
}
return arr.toArray(new Integer[0]);
}
class Node implements Comparable<Node>{
int pos;
int cnt;
public Node(int pos, int cnt){
this.pos = pos;
this.cnt = cnt;
}
public int compareTo(Node o){
if(this.cnt == o.cnt){
return this.pos - o.pos;
}
return o.cnt - this.cnt;
}
}
}
// 다른 사람의 풀이
public int[] solution2(int[] answer) {
int[] a = {1, 2, 3, 4, 5};
int[] b = {2, 1, 2, 3, 2, 4, 2, 5};
int[] c = {3, 3, 1, 1, 2, 2, 4, 4, 5, 5};
int[] score = new int[3];
for(int i=0; i<answer.length; i++) {
if(answer[i] == a[i%a.length]) {score[0]++;}
if(answer[i] == b[i%b.length]) {score[1]++;}
if(answer[i] == c[i%c.length]) {score[2]++;}
}
int maxScore = Math.max(score[0], Math.max(score[1], score[2]));
ArrayList<Integer> list = new ArrayList<>();
if(maxScore == score[0]) {list.add(1);}
if(maxScore == score[1]) {list.add(2);}
if(maxScore == score[2]) {list.add(3);}
return list.stream().mapToInt(i->i.intValue()).toArray();
}
각 학생이 문제를 찍는 패턴을 배열로 저장한다.
문제에서 제시한 기준으로 학생 번호를 정렬하기 위해 PriorityQueue를 생성한다.
각 패턴의 숫자와 정답 배열의 숫자가 같을 경우 해당 학생의 점수를 +1해준다.
각 학생의 정보를 우선순위큐에 담아준다.
조건에 따라 정렬된 큐에서 각 학생 정보를 꺼내며 리스트에 담아준다.
리스트를 Integer형 배열로 변환해 반환한다.
근데 너무 복잡하게 해결한거 같아 다른 사람의 풀이를 확인해봤는데 훨씬 간단한 풀이가 있었다ㅜㅜ