1단계 / 77484 / 로또의 최고 순위와 최저 순위

oh_eol·2021년 11월 2일
0

Programmers

목록 보기
1/7
post-thumbnail

문제 설명

로또를 구매한 민우는 당첨 번호 발표일을 학수고대하고 있었습니다. 하지만, 민우의 동생이 로또에 낙서를 하여, 일부 번호를 알아볼 수 없게 되었습니다. 당첨 번호 발표 후, 민우는 자신이 구매했던 로또로 당첨이 가능했던 최고 순위와 최저 순위를 알아보고 싶어 졌습니다.
알아볼 수 없는 번호를 0으로 표기하기로 하고, 민우가 구매한 로또 번호 6개가 44, 1, 0, 0, 31 25라고 가정해보겠습니다. 당첨 번호 6개가 31, 10, 45, 1, 6, 19라면, 당첨 가능한 최고 순위와 최저 순위의 한 예는 아래와 같습니다.

당첨 번호3110451619결과
최고 순위 번호310→104410→6254개 번호 일치, 3등
최저 순위 번호310→114410→7252개 번호 일치, 5등

순서와 상관없이, 구매한 로또에 당첨 번호와 일치하는 번호가 있으면 맞힌 걸로 인정됩니다.
알아볼 수 없는 두 개의 번호를 각각 10, 6이라고 가정하면 3등에 당첨될 수 있습니다.
3등을 만드는 다른 방법들도 존재합니다. 하지만, 2등 이상으로 만드는 것은 불가능합니다.
알아볼 수 없는 두 개의 번호를 각각 11, 7이라고 가정하면 5등에 당첨될 수 있습니다.
5등을 만드는 다른 방법들도 존재합니다. 하지만, 6등(낙첨)으로 만드는 것은 불가능합니다.
민우가 구매한 로또 번호를 담은 배열 lottos, 당첨 번호를 담은 배열 win_nums가 매개변수로 주어집니다. 이때, 당첨 가능한 최고 순위와 최저 순위를 차례대로 배열에 담아서 return 하도록 solution 함수를 완성해주세요.




코드

✔️ 내 풀이

import java.util.*;

class Solution {
    public int[] solution(int[] lottos, int[] win_nums) {
        int[] answer = new int[2];
        int countZero=0;
        int countCorrect=0;
        int highest, lowest;
        
        Arrays.sort(lottos);
        Arrays.sort(win_nums);
            
        //  0 개수 세기
        for(int i=0;i<lottos.length;i++) {
            if(lottos[i]!=0) break;
            countZero++;
        }
        // 맞은 개수 세기
        for(int i=0;i<win_nums.length;i++) {
            int key = win_nums[i];
            for(int j=0;j<lottos.length;j++) {
                if(key!=lottos[j]) continue;
                countCorrect++;
            }
        }
        
        highest=countCorrect+countZero;
        lowest=countCorrect;
        
        answer[0]=myLanking(highest);
        answer[1]=myLanking(lowest);
        
        return answer;
    }
        //  순위 반환하는 함수
    public static int myLanking(int correct) {
        switch(correct) {
            case 6: return 1;
            case 5: return 2;
            case 4: return 3;
            case 3: return 4;
            case 2: return 5;
            default: return 6;
            }
        }
}

🛠 참고 할 만 한 풀이

class Solution {
    public int[] solution(int[] lottos, int[] win_nums) {
        int zero = 0;
        int matched = 0;
        
        for (int l : lottos) {
            if (l == 0) zero++;
            else {
                for (int w : win_nums) {
                    if (l == w) {
                        matched++;
                        break;
                    }
                }
            }
        }
        
        int min = matched;
        int max = matched + zero;
        
        int[] answer = {Math.min(7 - max, 6), Math.min(7 - min, 6)};
        return answer;
    }
}
profile
공부 중입니다.

0개의 댓글