[programmers] 모의고사 (java) -1level

2022. 2. 27. 15:09Algorithm/Programmers

문제 설명

 

 

문제 해결

 

모든 값들을 다 거쳐서 확인해주면 되는 완탐 문제이다.

1번에서 3번 사람까지 정해져 있으니 people이라는 배열을 3의 크기로 생성하고 people 배열에 맞춘 문제 개수를 넣어주면 된다.

핵심은 각 반복되는 수를 arr 2차원 배열에 넣어주고 반복적으로 발생되는 값을 구현하는 것이다. (% 를 이용하여 반복되는 값을 넣어주자.)

 

import java.util.ArrayList;

public class 모의고사 {
    public int[] solution(int[] answers) {
        // 1번 12345 2번 21 23 24 25 반복 3번 33 11 22 44 55 반복
        int[][] arr = {
                {1,2,3,4,5},
                {2,1,2,3,2,4,2,5},
                {3,3,1,1,2,2,4,4,5,5}
        };
        int[] people = new int[3];

        for (int i = 0; i < people.length; i++){
            for (int j = 0; j < answers.length; j++){
                if(arr[i][j % arr[i].length] == answers[j]) people[j]++; // 구현
            }
        }

        int max = Math.max(Math.max(people[0],people[1]),people[2]);

        ArrayList<Integer> list = new ArrayList<>();

        if(max == people[0]) list.add(1);
        if(max == people[1]) list.add(2);
        if(max == people[2]) list.add(3);

        int[] answer = new int[list.size()];
        int idx = 0;
        for (int data: list
             ) {
            answers[idx] = data;
            idx++;
        }

        return answer;
    }
}

 

728x90