문서 색인

2. 코드

import java.util.List;
import java.util.ArrayList;

class Solution {
    public int[] solution(int[] answers) {
        int[] answer = {};
        int[] score = new int[3];
        int[][] person = {{ 1,2,3,4,5 }, { 2,1,2,3,2,4,2,5 }, { 3,3,1,1,2,2,4,4,5,5 }};

        for ( int i=0; i<person.length; i++ ) {                     
            for ( int a=0; a<answers.length; a++ ) {
                if ( person[i][a%person[i].length] == answers[a] ) {
                    score[i]++;
                }
            }

        }
        int maxScore = Math.max(score[0], Math.max(score[1], score[2]));
        List<Integer> result = new ArrayList<Integer>();

        if ( maxScore == score[0]) result.add(1);
        if ( maxScore == score[1]) result.add(2);
        if ( maxScore == score[2]) result.add(3);   

        answer = new int[result.size()];

        for ( int i=0; i<answer.length; i++ ) {
            answer[i] = result.get(i);
        }

        return answer;
    }
}