프로그래머스 - 모의고사

차관호·2023년 8월 20일

문제링크 - 프로그래머스 - 모의고사

import java.util.*;
class Solution {
    public int[] solution(int[] answers) {
        int[] person1 = {1, 2, 3, 4, 5}, person2 = {2, 1, 2, 3, 2, 4, 2, 5}, person3 = {3, 3, 1, 1, 2, 2, 4, 4, 5, 5};
        int sum1 = 0, sum2 = 0, sum3 = 0;
        int c1 = 0, c2 = 0, c3 = 0;

        for(int an : answers) {
            if (an == person1[c1]) {
                sum1++;
            }
            if (an == person2[c2]) {
                sum2++;
            }
            if (an == person3[c3]) {
                sum3++;
            }
            c1++;
            c2++;
            c3++;
            if (person1.length - 1 < c1) c1 = 0;
            if (person2.length - 1 < c2) c2 = 0;
            if (person3.length - 1 < c3) c3 = 0;
        }
        List<Integer> last = new ArrayList<>();
        int max = (sum1 > sum2) ? (sum1 > sum3 ? sum1 : sum3) : (sum2 > sum3 ? sum2 : sum3);
        int[] summ = {sum1,sum2,sum3};
        for(int i = 0; i < 3; i++){
            if(max == summ[i]) last.add(i+1);
        }
        Collections.sort(last);

        int[] realanswer = last.stream().mapToInt(Integer::intValue).toArray();
        
        return realanswer;

    }
}
profile
안녕하세요 :-)

0개의 댓글