알고리즘

프로그래머스 - 모의고사

계양 꿀주먹 2024. 4. 21. 08:11

 


 

import java.util.*;

class Solution {
    public int[] solution(int[] answers) {
        int[] p1 = {1, 2, 3, 4, 5};
        int[] p2 = {2, 1, 2, 3, 2, 4, 2, 5};
        int[] p3 = {3, 3, 1, 1, 2, 2, 4, 4, 5, 5};
        int[] scores = new int[3];
        int max = 0;
        List<Integer> list = new ArrayList<>();
        
        for(int i = 0; i < answers.length; i++) {
            if(answers[i] == p1[i % p1.length]) scores[0]++;
            if(answers[i] == p2[i % p2.length]) scores[1]++;
            if(answers[i] == p3[i % p3.length]) scores[2]++;
        }

        for(int score : scores) {
            if(max < score) max = score;    
        }
        
        for(int i = 0; i < scores.length; i++){
            if(max == scores[i]) list.add(i + 1);
        }
        
        return list.stream().mapToInt(i -> i).toArray();
    }
}

 

각 수포자의 찍기 패턴을 배열로 선언 후, 나머지 연산자를 이용하여 반복하도록 하여 점수를 구합니다.

이후 가장 점수를 잘 맞은 수포자의 점수를 구해 점수와 같은 사람들을 찾아 리스트에 더해 배열로 바꾸어줬습니다.