본문 바로가기
코딩테스트 문제집/Programmers(Lv1)

[Programmers] Lv1: 모의고사(42840)

by cogito30 2025. 2. 11.
반응형

문제

- 링크: https://school.programmers.co.kr/learn/courses/30/lessons/42840

 

풀이

더보기
더보기
import java.util.ArrayList;
import java.util.Arrays;


class Solution {
    public int[] solution(int[] answers) {
        int[][] pattern = {
            {1, 2, 3, 4, 5},
            {2, 1, 2, 3, 2, 4, 2, 5},
            {3, 3, 1, 1, 2, 2, 4, 4, 5, 5}
        };
        
        int[] scores = new int[3];
        
        for (int i = 0; i < answers.length; ++i) {
            for (int j = 0; j < pattern.length; ++j) {
                if (answers[i] == pattern[j][i % pattern[j].length]) {
                    scores[j]++;
                }
            }
        }
        
        int maxScore = Arrays.stream(scores).max().getAsInt();
        
        ArrayList<Integer> answer = new ArrayList();
        for (int i = 0; i < scores.length; ++i) {
            if (scores[i] == maxScore) {
                answer.add(i + 1);
            }
        }
        
        return answer.stream().mapToInt(Integer::intValue).toArray();
    }
}

 

반응형