/ PROBLEMS

[Programmers] 모의고사

Note : 이 글은 지극히 주관적인 생각을 토대로 작성된 글입니다. 혹시나 잘못된 부분이 있다면 메일 또는 코멘트를 통해 알려주시면 감사하겠습니다. 😄 제 메일은 About 탭에서 확인하실 수 있습니다. 📧

P.S : 이 페이지는 웹에 최적화 된 페이지입니다. 가급적 모바일이 아닌 웹에서 보시는 것을 추천드립니다.

문제 정보

  • 문제 설명

    • 수포자는 수학을 포기한 사람의 준말입니다. 수포자 삼인방은 모의고사에 수학 문제를 전부 찍으려 합니다. 수포자는 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문제씩을 맞췄습니다.

소스코드

  • 문제 풀이 언어 : Java
class Solution {
    public int[] solution(int[] answers) {
        int[] one = new int[]{1, 2, 3, 4, 5};
        int[] two = new int[]{2, 1, 2, 3, 2, 4, 2, 5};
        int[] three = new int[]{3, 3, 1, 1, 2, 2, 4, 4, 5, 5};

        int oneCount = 0;
        int twoCount = 0;
        int threeCount = 0;

        int oneIndex = 0;
        int twoIndex = 0;
        int threeIndex = 0;

        for(int i=0; i<answers.length; i++) {
            if (answers[i] == one[oneIndex]) {
                oneCount += 1;
            }

            if (answers[i] == two[twoIndex]) {
                twoCount += 1;
            }

            if (answers[i] == three[threeIndex]) {
                threeCount += 1;
            }

            oneIndex++;
            twoIndex++;
            threeIndex++;

            if(oneIndex == one.length)
                oneIndex = 0;
            if (twoIndex == two.length)
                twoIndex = 0;
            if (threeIndex == three.length)
                threeIndex = 0;
        }

        if (oneCount == twoCount && twoCount == threeCount)
            return new int[]{1, 2, 3};
        else if (oneCount > twoCount && oneCount > threeCount)
            return new int[]{1};
        else if (oneCount == twoCount && twoCount > threeCount)
            return new int[]{1, 2};
        else if (oneCount == threeCount && threeCount > twoCount)
            return new int[]{1, 3};
        else if (twoCount > oneCount && twoCount > threeCount)
            return new int[]{2};
        else if (threeCount > twoCount && threeCount > oneCount)
            return new int[]{3};
        else
            return new int[]{2, 3};
    }
}