Light Blue Pointer
본문 바로가기
Coding Test

[프로그래머스]모의고사 완전탐색

by Greedy 2023. 11. 29.

문제 주소

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

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

문제 설명

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

풀이과정

class Solution {
    public int[] solution(int[] answers) {
        int [] pattern1 = {1,2,3,4,5};
        int [] pattern2 = {2, 1, 2, 3, 2, 4, 2, 5};
        int [] pattern3 = {3, 3, 1, 1, 2, 2, 4, 4, 5, 5};
        
        int correct1 = 0;
        int correct2 = 0;
        int correct3 = 0;
        
        int index1 = 0;
        int index2 = 0;
        int index3 = 0;
        
        for(int i = 0;i<answers.length;i++){
            if(answers[i]==pattern1[index1]){
                correct1++;
            }
            if(answers[i]==pattern2[index2]){
                correct2++;
            }
            if(answers[i]==pattern3[index3]){
                correct3++;
            }
            
            if(index1 == pattern1.length-1){
                index1 = 0;
            }
            if(index2 == pattern2.length-1){
                index2 = 0;
            }
            if(index3 == pattern3.length-1){
                index3 = 0;
            }
        }
        if(correct1>correct2){//2제외
            if(correct1>correct3){//3제외
                int[] answer = {2};
                return answer;
            }else if(correct1==correct3){
                int[] answer = {1,3};
                return answer;
            }else if(correct1<correct3){//1제외
                int[] answer = {3};
                return answer;
            }    
        }else if(correct1==correct2){
            if(correct1>correct3){//3제외
                int[] answer = {1,2};
                return answer;
            }else if(correct1==correct3){
                int[] answer = {1,2,3};
                return answer;
            }else if(correct1<correct3){//1제외
                int[] answer = {3};
                return answer;
            }
            
        }
        else if(correct1<correct2){//1제외
            if(correct2>correct3){//3제외
                int[] answer = {2};
                return answer;
            }else if(correct2==correct3){
                int[] answer = {2,3};
                return answer;
            }else if(correct2<correct3){//2제외
                int[] answer = {3};
                return answer;
            }
        }
        return null;
    }
}

문제는 금방 풀었는데 돌려보내는 과정이 대단한 노가다…

그러고 나서 또 문제가

테스트 1
입력값 〉[1, 2, 3, 4, 5]
기댓값 〉[1]
실행 결과 〉실행한 결괏값 [1,2,3]이 기댓값 [1]과 다릅니다.
테스트 2
입력값 〉[1, 3, 2, 4, 2]
기댓값 〉[1, 2, 3]
실행 결과 〉실행한 결괏값 [2]이 기댓값 [1,2,3]과 다릅니다.

답이 틀렸다는거 ㅎ

로그 찍어봄

answers[i]1
pattern1[index1]1
correct10
pattern2[index2]2
correct20
pattern3[index3]3
correct30
answers[i]2
pattern1[index1]1
correct11
pattern2[index2]2
correct20
pattern3[index3]3
correct30
answers[i]3
pattern1[index1]1
correct11
pattern2[index2]2
correct21
pattern3[index3]3
correct30
answers[i]4
pattern1[index1]1
correct11
pattern2[index2]2
correct21
pattern3[index3]3
correct31
answers[i]5
pattern1[index1]1
correct11
pattern2[index2]2
correct21
pattern3[index3]3
correct31

보기 불편해서 pattern을 전에 찍고

correct를 후에 찍음

answers[i]1
pattern1[index1]1
pattern2[index2]2
pattern3[index3]3
correct11
correct20
correct30
answers[i]2
pattern1[index1]1
pattern2[index2]2
pattern3[index3]3
correct11
correct21
correct30
answers[i]3
pattern1[index1]1
pattern2[index2]2
pattern3[index3]3
correct11
correct21
correct31
answers[i]4
pattern1[index1]1
pattern2[index2]2
pattern3[index3]3
correct11
correct21
correct31
answers[i]5
pattern1[index1]1
pattern2[index2]2
pattern3[index3]3
correct11
correct21
correct31

헐… pattern이 안 넘어감 알고보니 내가 인덱스를 안 증가시키고 있었음 ㅋㅋㅋㅋㅋㅋㅋ

class Solution {
    public int[] solution(int[] answers) {
        int [] pattern1 = {1,2,3,4,5};
        int [] pattern2 = {2, 1, 2, 3, 2, 4, 2, 5};
        int [] pattern3 = {3, 3, 1, 1, 2, 2, 4, 4, 5, 5};
        
        int correct1 = 0;
        int correct2 = 0;
        int correct3 = 0;
        
        int index1 = 0;
        int index2 = 0;
        int index3 = 0;
        
        /*맞은 개수 count*/
        for(int i = 0;i<answers.length;i++){
            System.out.println("answers[i]"+answers[i]);
            System.out.println("pattern1[index1]"+pattern1[index1]);
            System.out.println("pattern2[index2]"+pattern2[index2]);
            System.out.println("pattern3[index3]"+pattern3[index3]);

            if(answers[i]==pattern1[index1]){
                correct1++;
            }
            if(answers[i]==pattern2[index2]){
                correct2++;
            }
            if(answers[i]==pattern3[index3]){
                correct3++;
            }
            
            if(index1 == pattern1.length-1){
                index1 = 0;
            }
            if(index2 == pattern2.length-1){
                index2 = 0;
            }
            if(index3 == pattern3.length-1){
                index3 = 0;
            }
            
            index1++;
            index2++;
            index3++;
            
            System.out.println("correct1"+correct1);
            System.out.println("correct2"+correct2);
            System.out.println("correct3"+correct3);
        }
        
        /*리턴할 값 결정*/
        if(correct1>correct2){//2제외
            if(correct1>correct3){//3제외
                int[] answer = {2};
                return answer;
            }else if(correct1==correct3){
                int[] answer = {1,3};
                return answer;
            }else if(correct1<correct3){//1제외
                int[] answer = {3};
                return answer;
            }    
        }else if(correct1==correct2){
            if(correct1>correct3){//3제외
                int[] answer = {1,2};
                return answer;
            }else if(correct1==correct3){
                int[] answer = {1,2,3};
                return answer;
            }else if(correct1<correct3){//1제외
                int[] answer = {3};
                return answer;
            }
        }
        else if(correct1<correct2){//1제외
            if(correct2>correct3){//3제외
                int[] answer = {2};
                return answer;
            }else if(correct2==correct3){
                int[] answer = {2,3};
                return answer;
            }else if(correct2<correct3){//2제외
                int[] answer = {3};
                return answer;
            }
        }
        return null;
    }
}

실행 결과 하나는 되고 하나는 안 됨

테스트 1
입력값 〉	[1, 2, 3, 4, 5]
기댓값 〉	[1]
실행 결과 〉	실행한 결괏값 [2]이 기댓값 [1]과 다릅니다.
출력 〉	
answers[i]1
pattern1[index1]1
pattern2[index2]2
pattern3[index3]3
**correct11**
correct20
correct30
answers[i]2
pattern1[index1]2
pattern2[index2]1
pattern3[index3]3
**correct12**
correct20
correct30
answers[i]3
pattern1[index1]3
pattern2[index2]2
pattern3[index3]1
**correct13**
correct20
correct30
answers[i]4
pattern1[index1]4
pattern2[index2]3
pattern3[index3]1
**correct14**
correct20
correct30
answers[i]5
pattern1[index1]5
pattern2[index2]2
pattern3[index3]2
**correct15**
correct20
correct30

-> 아니 correct1만 5고 나머지가 0인데 어케 이렇게...
 return 부분이 잘못됐나봄

테스트 2
입력값 〉	[1, 3, 2, 4, 2]
기댓값 〉	[1, 2, 3]
실행 결과 〉	테스트를 통과하였습니다.
출력 〉	
answers[i]1
pattern1[index1]1
pattern2[index2]2
pattern3[index3]3
correct11
correct20
correct30
answers[i]3
pattern1[index1]2
pattern2[index2]1
pattern3[index3]3
correct11
correct20
correct31
answers[i]2
pattern1[index1]3
pattern2[index2]2
pattern3[index3]1
correct11
correct21
correct31
answers[i]4
pattern1[index1]4
pattern2[index2]3
pattern3[index3]1
correct12
correct21
correct31
answers[i]2
pattern1[index1]5
pattern2[index2]2
pattern3[index3]2
correct12
correct22
correct32

보니까 동작은 똑바로 하고 개수 카운트도 잘 하는데 return 부분이 잘못된 거 같음

if(correct1>correct2){//2제외
            if(correct1>correct3){//3제외
                int[] answer = {2};
                return answer;

2제외하고 3제외했으면 1남아야지 왜 2가 남아있는거임

이 부분 수정함

테스트케이스 통과함

근데 제출했더니 오류남ㅋㅋㅋㅋㅋㅋ

테스트 1 〉 실패 (0.02ms, 82.8MB)

테스트 2 〉 통과 (0.02ms, 76.5MB)
테스트 3 〉 통과 (0.02ms, 77.8MB)
테스트 4 〉 통과 (0.01ms, 75.7MB)
테스트 5 〉 실패 (0.02ms, 77.1MB)
테스트 6 〉 실패 (0.03ms, 73.8MB)
테스트 7 〉 실패 (0.34ms, 69MB)
테스트 8 〉 통과 (0.13ms, 77.1MB)
테스트 9 〉 실패 (0.99ms, 74.8MB)
테스트 10 〉 실패 (0.30ms, 74.7MB)
테스트 11 〉 실패 (0.65ms, 76.5MB)
테스트 12 〉 실패 (0.62ms, 74.1MB)
테스트 13 〉 실패 (0.05ms, 73.9MB)
테스트 14 〉 통과 (0.64ms, 77.2MB)

분류 케이스는 틀릴수가 없는디 보면…

리턴부분이 잘못된 거 같음

총 7개의 케이스가 나와야 함

1,2,3, 12,23,13,123

Math.max돌려서 나온 숫자랑 일치하는 숫자들 돌려보내자 걍 ㅋㅋ

에휴 ArrayList도 import안 하려고 했는데 걍 해야함

자료구조가 편한게 있으면 잘 쓰시면 됩니다 자꾸 알고리즘으로 모든걸 해결하려 하지 말고…

⚠️import java.util.Math; 이거 아님 import java.lang.Math;

import java.lang.Math;
import java.util.ArrayList;
class Solution {
    public int[] solution(int[] answers) {
        int [] pattern1 = {1,2,3,4,5};
        int [] pattern2 = {2, 1, 2, 3, 2, 4, 2, 5};
        int [] pattern3 = {3, 3, 1, 1, 2, 2, 4, 4, 5, 5};
        
        int correct1 = 0;
        int correct2 = 0;
        int correct3 = 0;
        
        int index1 = 0;
        int index2 = 0;
        int index3 = 0;
        
        /*맞은 개수 count*/
        for(int i = 0;i<answers.length;i++){

            if(answers[i]==pattern1[index1]){
                correct1++;
            }
            if(answers[i]==pattern2[index2]){
                correct2++;
            }
            if(answers[i]==pattern3[index3]){
                correct3++;
            }
            
            if(index1 == pattern1.length-1){
                index1 = 0;
            }
            if(index2 == pattern2.length-1){
                index2 = 0;
            }
            if(index3 == pattern3.length-1){
                index3 = 0;
            }
            
            index1++;
            index2++;
            index3++;

        }
        
        /*리턴할 값 결정*/
        int max = Math.max(correct1,correct2);
        max = Math.max(max,correct3);
        ArrayList<Integer> array = new ArrayList<Integer>();
        if(correct1==max){
            array.add(1);
        }
        if(correct2==max){
            array.add(2);
        }
        if(correct3==max){
            array.add(3);
        }
        int [] answer = new int[array.size()];
        for(int i = 0; i<answer.length; i++){
            answer[i]=array.get(i);
        }
        
        return answer;
    }
}

이러고 또 에러남

테스트 1 〉 실패 (0.04ms, 76.6MB)

테스트 2 〉 통과 (0.10ms, 78MB)
테스트 3 〉 통과 (0.05ms, 73.5MB)
테스트 4 〉 통과 (0.04ms, 74.5MB)
테스트 5 〉 실패 (0.05ms, 77.4MB)
테스트 6 〉 실패 (0.04ms, 76.1MB)
테스트 7 〉 실패 (0.36ms, 73.3MB)
테스트 8 〉 통과 (0.16ms, 75.3MB)
테스트 9 〉 실패 (1.13ms, 72.7MB)
테스트 10 〉 실패 (0.31ms, 76.7MB)
테스트 11 〉 실패 (1.23ms, 78.6MB)
테스트 12 〉 실패 (0.73ms, 76.1MB)
테스트 13 〉 실패 (0.14ms, 72.6MB)
테스트 14 〉 통과 (0.71ms, 72.2MB)

엥 아까랑 똑같은걸 보니까 리턴부분이 문제가 아닌가?

if(index1 == pattern1.length-1){
                index1 = 0;
            }
            if(index2 == pattern2.length-1){
                index2 = 0;
            }
            if(index3 == pattern3.length-1){
                index3 = 0;
            }
            index1++;
            index2++;
            index3++;

다시 살펴보니 이 부분이 좀 이상함

0으로 만들고 다시 더해주고 있음

/*맞은 개수 count*/
        for(int i = 0;i<answers.length;i++){

            if(answers[i]==pattern1[index1]){
                correct1++;
            }
            if(answers[i]==pattern2[index2]){
                correct2++;
            }
            if(answers[i]==pattern3[index3]){
                correct3++;
            }
            
            index1++;
            index2++;
            index3++;
            
            if(index1 == pattern1.length){
                index1 = 0;
            }
            if(index2 == pattern2.length){
                index2 = 0;
            }
            if(index3 == pattern3.length-){
                index3 = 0;
            }
        }

이렇게 바꿈

테스트 1 〉 실패 (런타임 에러)

테스트 2 〉 실패 (런타임 에러)
테스트 3 〉 실패 (런타임 에러)
테스트 4 〉 실패 (런타임 에러)
테스트 5 〉 실패 (런타임 에러)
테스트 6 〉 실패 (런타임 에러)
테스트 7 〉 실패 (런타임 에러)
테스트 8 〉 실패 (런타임 에러)
테스트 9 〉 실패 (런타임 에러)
테스트 10 〉 실패 (런타임 에러)
테스트 11 〉 실패 (런타임 에러)
테스트 12 〉 실패 (런타임 에러)
테스트 13 〉 실패 (런타임 에러)
테스트 14 〉 실패 (런타임 에러)
/*맞은 개수 count*/
        for(int i = 0;i<answers.length;i++){

            if(answers[i]==pattern1[index1]){
                correct1++;
            }
            if(answers[i]==pattern2[index2]){
                correct2++;
            }
            if(answers[i]==pattern3[index3]){
                correct3++;
            }

            if(index1 == pattern1.length-1){
                index1 = 0;
            }else{
                index1++;
            }
            if(index2 == pattern2.length-1){
                index2 = 0;
            }else{
                index2++;
            }
            if(index3 == pattern3.length-1){
                index3 = 0;
            }else{
                index3++;
            }
        }

이렇게 바꿔봄

 통과함 근데 저 둘의 차이점을 모르겠네?

내 눈에는 둘 다 되는걸로 보임

-> 나중에 보니까 내가if(index3 == pattern3.length-){

이부분에 - 오타내놔서 그런거였다

 

제출 코드

import java.lang.Math;
import java.util.ArrayList;
class Solution {
    public int[] solution(int[] answers) {
        int [] pattern1 = {1,2,3,4,5};
        int [] pattern2 = {2, 1, 2, 3, 2, 4, 2, 5};
        int [] pattern3 = {3, 3, 1, 1, 2, 2, 4, 4, 5, 5};
        
        int correct1 = 0;
        int correct2 = 0;
        int correct3 = 0;
        
        int index1 = 0;
        int index2 = 0;
        int index3 = 0;
        
        /*맞은 개수 count*/
        for(int i = 0;i<answers.length;i++){

            if(answers[i]==pattern1[index1]){
                correct1++;
            }
            if(answers[i]==pattern2[index2]){
                correct2++;
            }
            if(answers[i]==pattern3[index3]){
                correct3++;
            }
            
            index1++;
            index2++;
            index3++;
            
            if(index1 == pattern1.length){
                index1 = 0;
            }
            if(index2 == pattern2.length){
                index2 = 0;
            }
            if(index3 == pattern3.length){
                index3 = 0;
            }

        }

        
        /*리턴할 값 결정*/
        int max = Math.max(correct1,correct2);
        max = Math.max(max,correct3);
        ArrayList<Integer> array = new ArrayList<Integer>();
        if(correct1==max){
            array.add(1);
        }
        if(correct2==max){
            array.add(2);
        }
        if(correct3==max){
            array.add(3);
        }
        int [] answer = new int[array.size()];
        for(int i = 0; i<answer.length; i++){
            answer[i]=array.get(i);
        }
        return answer;
    }
}