Notice
Recent Posts
Recent Comments
Link
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
Tags more
Archives
Today
Total
관리 메뉴

공부블로그

[프로그래머스] 로또의 최고 순위와 최저 순위 본문

IT/알고리즘

[프로그래머스] 로또의 최고 순위와 최저 순위

So1_b 2022. 6. 15. 22:52

https://programmers.co.kr/learn/courses/30/lessons/77484

 

코딩테스트 연습 - 로또의 최고 순위와 최저 순위

로또 6/45(이하 '로또'로 표기)는 1부터 45까지의 숫자 중 6개를 찍어서 맞히는 대표적인 복권입니다. 아래는 로또의 순위를 정하는 방식입니다. 1 순위 당첨 내용 1 6개 번호가 모두 일치 2 5개 번호

programmers.co.kr

 

import java.util.*;
class Solution {
    public int[] solution(int[] lottos, int[] win_nums) {
        int count=0;
        int[] answer = new int[2];
        
        HashSet<Integer> lotto  = new HashSet<Integer>();
        HashSet<Integer> win    = new HashSet<Integer>();
        
        for(int n: lottos) {
            if(n==0)  count++;    
            lotto.add(n);
        }
        for(int n: win_nums){
            win.add(n);
        }
        lotto.retainAll(win);
        int correct = lotto.size(); //맞힌 개수
        
        for(int i=0; i<answer.length; i++){ //answer[0], [1]
            int num=7;
            if(i==0) num -= (correct + count);  //최고 순위
            else     num -= correct;            //최저 순위    
            
            if(num==7) num=6;
            answer[i]=num;
        }
        
        return answer;
    }
}

알게 된 점

- Arrays.asList()를 이용해 배열을 Set으로 변경하려고 했으나 오류.

이유:

lottos, win_nums 배열은 int[ ]형이라 위 메서드를 적용시키면 List<int[ ]>을 반환. 

으로 HashSet<Integer>(Collection<? extends Integer> c)이나 다름 없으나 List<int[ ]>에서 int[]은 <- ->Integer/ Object변환이 불가능하기 때문에 오류가 났음. 

 따라서 Integer[ ]을 asList()의 매개변수로 주면 사용가능.

Comments