우주먼지

💡 문제 파악

수포자는 수학을 포기한 사람의 준말입니다. 수포자 삼인방은 모의고사에 수학 문제를 전부 찍으려 합니다. 수포자는 1번 문제부터 마지막 문제까지 다음과 같이 찍습니다.

1번 수포자가 찍는 방식: 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, ...
2번 수포자가 찍는 방식: 2, 1, 2, 3, 2, 4, 2, 5, 2, 1, 2, 3, 2, 4, 2, 5, ...
3번 수포자가 찍는 방식: 3, 3, 1, 1, 2, 2, 4, 4, 5, 5, 3, 3, 1, 1, 2, 2, 4, 4, 5, 5, ...

1번 문제부터 마지막 문제까지의 정답이 순서대로 들은 배열 answers가 주어졌을 때, 가장 많은 문제를 맞힌 사람이 누구인지 배열에 담아 return 하도록 solution 함수를 작성해주세요.

 

제한 조건

  • 시험은 최대 10,000 문제로 구성되어있습니다.
  • 문제의 정답은 1, 2, 3, 4, 5중 하나입니다.
  • 가장 높은 점수를 받은 사람이 여럿일 경우, return하는 값을 오름차순 정렬해주세요.

 

입출력 예

answers return
[1,2,3,4,5] [1]
[1,3,2,4,2] [1,2,3]

 

입출력 예 설명

입출력 예 #1

  • 수포자 1은 모든 문제를 맞혔습니다.
  • 수포자 2는 모든 문제를 틀렸습니다.
  • 수포자 3은 모든 문제를 틀렸습니다.

따라서 가장 문제를 많이 맞힌 사람은 수포자 1입니다.

 

입출력 예 #2

  • 모든 사람이 2문제씩을 맞췄습니다.

 

풀이

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;

public class Solution {
    static int[] testA = {1,2,3,4,5};
    static int[] testB = {2,1,2,3,2,4,2,5};
    static int[] testC = {3,3,1,1,2,2,4,4,5,5};

    static int[] answer = {1,2,3,4,5};

    public static void main(String[] args) {
        solution(answer);
    }

    public static int[] solution(int[] answers) {
        int[] score = new int[3];

        HashMap<Integer, Integer> map = new HashMap<>();
        map.put(1, 0);
        map.put(2, 0);
        map.put(3, 0);

        // 정답 갯수 Count, Key(학생번호):Value(정답개수) 설정
        for (int i = 0; i < answers.length; i++) {
            int num = answers[i];
            if (testA[i%5] == num) map.replace(1, map.get(1)+1);
            if (testB[i%8] == num) map.replace(2, map.get(2)+1);
            if (testC[i%10] == num) map.replace(3, map.get(3)+1);
        }
        // HashMap의 Value를 돌면서 최대 값 구하기
        int max = map.get(1);
        for (Integer val : map.values()) {
            if (val > max) {
                max = val;
            }
        }
        // 최대값과 일치하는 Key를 정답 리스트에 저장
        ArrayList<Integer> answerList = new ArrayList<>();
        for (Integer key : map.keySet()) {
            if (map.get(key) == max) {
                answerList.add(key);
            }
        }

        // 오름차순 정렬 및 배열로 변환후 반환
        int[] answer = new int[answerList.size()];
        Collections.sort(answerList);
        for (int i = 0; i < answer.length; i++) {
            answer[i] = answerList.get(i);
        }
        System.out.println(Arrays.toString(answer));
        return answer;
    }
}
profile

우주먼지

@o귤o

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!

검색 태그