프로그래머스 모의고사 자바

바그다드·2023년 10월 2일
0

문제

풀이

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();
    }

리뷰

  1. 각 학생이 문제를 찍는 패턴을 배열로 저장한다.

  2. 문제에서 제시한 기준으로 학생 번호를 정렬하기 위해 PriorityQueue를 생성한다.

    • 이를 위해 학생의 정보를 저장할 Node클래스를 생성하고 Comparable을 구현해준다.
      compareTo()메서드를 오버라이드 해주면 된다.
    • return값이 음수일 경우 오름차순, 양수일 경우 내림차순 정렬을 할 수 있다.
  3. 각 패턴의 숫자와 정답 배열의 숫자가 같을 경우 해당 학생의 점수를 +1해준다.

    • 각 패턴의 길이가 다르므로 패턴의 길이와 i를 나머지 연산하여 각 인덱스를 맞춰준다.
    • 0을 나머지 연산할 경우 예외가 발생하므로 0번만 따로 if연산해준다.
  4. 각 학생의 정보를 우선순위큐에 담아준다.

  5. 조건에 따라 정렬된 큐에서 각 학생 정보를 꺼내며 리스트에 담아준다.

  6. 리스트를 Integer형 배열로 변환해 반환한다.

    • 여기서 파라미터로 new Integer[0]을 넘겨줬는데, 이 파라미터는 리스트에 담긴 배열의 길이가 파라미터의 길이보다 짧으면 리스트의 길이만큼 배열을 생성하고 아닐경우 파라미터의 길이만큼 배열을 생성해 반환한다.

근데 너무 복잡하게 해결한거 같아 다른 사람의 풀이를 확인해봤는데 훨씬 간단한 풀이가 있었다ㅜㅜ

profile
꾸준히 하자!

0개의 댓글