본문 바로가기

프로그래머스

프로그래머스 - 모의고사

import java.util.*;
class Solution {
    public int[] solution(int[] answers) {
        // 초기값 세팅
        int [] first = {1,2,3,4,5};
        int [] second = {2,1,2,3,2,4,2,5};
        int [] third = {3,3,1,1,2,2,4,4,5,5};

        int firstCount = 0;
        int secondCount = 0;
        int thirdCount = 0;
        for (int i=0; i<answers.length; i++) {
            if(first[i%first.length] == answers[i]){
                firstCount++;
            }
        }
        for (int i=0; i<answers.length; i++) {
            if(second[i%second.length] == answers[i]){
                secondCount++;
            }
        }
        for (int i=0; i<answers.length; i++) {
            if(third[i%third.length] == answers[i]){
                thirdCount++;
            }
        }
        System.out.println(firstCount);
        System.out.println(secondCount);
        System.out.println(thirdCount);
        int max = 0;
        ArrayList <Integer> savePot = new ArrayList<>();
        max = Math.max(Math.max(firstCount,secondCount),thirdCount);
        if(firstCount == max){
            savePot.add(1);
        }
        if(secondCount == max){
            savePot.add(2);

        }
        if(thirdCount == max){
            savePot.add(3);
        }
       return savePot.stream().mapToInt(Integer::intValue).toArray();
    }
}