본문 바로가기

ProgramSoliving

프로그래머스 : 모의고사

반응형
import java.util.*;

class Solution {
    static int h1[] ={1,2,3,4,5};
    static int h2[] ={2,1,2,3,2,4,2,5};
    static int h3[] ={3,3,1,1,2,2,4,4,5,5};
    
    public int[] solution(int[] answers) {
        int answer[] = new int[3];
        int h1Cnt = 0;
        int h2Cnt = 0;
        int h3Cnt = 0;
        int index = 0;
        for(int i=0;i<answers.length;i++){
            h1Cnt = h1Cnt + (h1[i%h1.length] == answers[i] ? 1 : 0);
            h2Cnt = h2Cnt + (h2[i%h2.length] == answers[i] ? 1 : 0);
            h3Cnt = h3Cnt + (h3[i%h3.length] == answers[i] ? 1 : 0);
        }
        
        int MaxCnt = Math.max(h1Cnt,Math.max(h2Cnt,h3Cnt));
        if(MaxCnt==h1Cnt)
            answer[index++] =1;
        
        if(MaxCnt==h2Cnt)
            answer[index++] =2;
        
        if(MaxCnt==h3Cnt)
            answer[index++] =3;
        
        return Arrays.stream(answer).filter(i -> i!=0).toArray();
    }
}
반응형