본문 바로가기
Beakjoon&프로그래머스/Java

[프로그래머스/Java] 모의고사

by 현장 2025. 2. 26.

-Code

import java.util.Arrays;
import java.util.stream.IntStream;

class Solution {
    public int[] solution(int[] answers) {
        int[] cnt = {0, 0, 0};
        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 < answers.length; i++) {
            for (int j = 0; j < 3; j++) {
                cnt[j] += person[j][i % person[j].length] == answers[i] ?
                        1 : 0;
            }
        }
        int max = Arrays.stream(cnt).max().orElse(0);
        return IntStream.range(0, 3).filter(i -> cnt[i] == max)
                .map(i -> i + 1).toArray();
    }
}