자료구조, 코딩테스트/완전탐색(Exhaustive Search)

[코딩테스트] Programmers - 모의고사 (실패)

RꞮbble 2026. 6. 4. 20:57

 

https://school.programmers.co.kr/learn/courses/30/lessons/42840

 

 

풀이

문제 = 1번 ~ 마지막 문제까지 정답이 들은 배열 answer가 주어졌을 때, 가장 많은 문제를 맞힌 사람을 배열에 담아 반환
       

 

제한 조건

  • answer 배열 길이는 최대 10,000
  • 문제 정답은 1 ~ 5 중 하나
  • 가장 높은 점수 받은 사람이 여럿일 경우, return 값을 오름차순 정렬하기.        

 

풀이

1번 수포자 = (1 2 3 4 5) (1 2 3 4 5) ... = 1 ~ 5번까지 순서대로 찍기.
2번 수포자 = (2 1 2 3) (2 4 2 5) (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) ... = (3 3) (1 1) (2 2) (4 4) (5 5) ... 순서대로 찍기. 

 

  • 1~3번 수포자의 정답지를 배열에 저장하기. (arr1, arr2, arr3)
  • answer배열에서 하나씩 읽으며 1~3번 정답지 배열과 비교하며 맞춘 횟수를
    (int index, int count) answer1, (int index, int count) answer2, (int index, int count) answer3에 저장
  • answer1 ~ answer3을 오름차순 정렬
  • 최댓값이 여러개이면 순서대로 answer배열에 넣기.
  • 최댓값이 하나이면 하나만 answer배열에 넣기.
  • answer배열 출력 

 

 

코드1 - 오답

using System;
using System.Linq;
using System.Collections.Generic;

public class Solution {
    public int[] solution(int[] answers) 
    {
        int[] arr1 = {1, 2, 3, 4, 5};
        int index1 = 0; 
        int answer1 = 0; 
        
        int[] arr2 = {2, 1, 2, 3, 2, 4, 2, 5};
        int index2 = 0; 
        int answer2 = 0; 
        
        int[] arr3 = {3, 3, 1, 1, 2, 2, 4, 4, 5, 5};
        int index3 = 0; 
        int answer3 = 0; 
        
        for(int i = 0; i < answers.Length; i++)
        {
            if(answers[i] == arr1[index1++])
            {
                answer1++;
                if(index1 >= arr1.Length)
                    index1 = 0; 
            }
            
            if(answers[i] == arr2[index2++])
            {
                answer2++;
                if(index2 >= arr2.Length)
                    index2 = 0; 
            }
            
            if(answers[i] == arr3[index3++])
            {
                answer3++;
                if(index3 >= arr3.Length)
                    index3 = 0; 
            }
        }
        
        (int index, int count) a1;
        a1.index = 1; 
        a1.count = answer1;
        
        (int index, int count) a2;
        a2.index = 2;
        a2.count = answer2;
        
        (int index, int count) a3;
        a3.index = 3;
        a3.count = answer3; 
        
        List<(int index, int count)> list = new List<(int index, int count)>();
        list.Add(a1);
        list.Add(a2);
        list.Add(a3); 
        
        int max = list.Max(x => x.count); 
        
        return list.Where(a => a.count == max).OrderBy(a => a.index).Select(a => a.index).ToArray();
    }
}

 

LINQ를 사용해서 풀어봤다. 

인덱스 범위에서 벗어나면 인덱스를 0으로 만들어주는 부분이 정답을 맞추었을 때만 처리가 된다. 

정답을 맞추지 않으면 인덱스는 계속 늘어나기만 하고 배열 크기보다 더 큰 인덱스로 배열에 접근하니 에러가 발생한다. 

 

 

코드2 - 정답

using System;
using System.Linq;
using System.Collections.Generic;

public class Solution {
    public int[] solution(int[] answers) 
    {
        int[] arr1 = {1, 2, 3, 4, 5};
        int index1 = 0; 
        int answer1 = 0; 
        
        int[] arr2 = {2, 1, 2, 3, 2, 4, 2, 5};
        int index2 = 0; 
        int answer2 = 0; 
        
        int[] arr3 = {3, 3, 1, 1, 2, 2, 4, 4, 5, 5};
        int index3 = 0; 
        int answer3 = 0; 
        
        for(int i = 0; i < answers.Length; i++)
        {
            if(answers[i] == arr1[index1++])
                answer1++;
            if(index1 >= arr1.Length)
                    index1 = 0; 
            
            if(answers[i] == arr2[index2++])
                answer2++;
            if(index2 >= arr2.Length)
                    index2 = 0; 
            
            if(answers[i] == arr3[index3++])
                answer3++;
            if(index3 >= arr3.Length)
                    index3 = 0; 
        }
        
        (int index, int count) a1;
        a1.index = 1; 
        a1.count = answer1;
        
        (int index, int count) a2;
        a2.index = 2;
        a2.count = answer2;
        
        (int index, int count) a3;
        a3.index = 3;
        a3.count = answer3; 
        
        List<(int index, int count)> list = new List<(int index, int count)>();
        list.Add(a1);
        list.Add(a2);
        list.Add(a3); 
        
        int max = list.Max(x => x.count); 
        
        return list.Where(a => a.count == max).OrderBy(a => a.index).Select(a => a.index).ToArray();
    }
}

 

정답을 맞추든 맞추지 못하든 인덱스 범위 체크를 해주도록 코드를 수정해주니 통과했다.