Java/Algorithm

[Algorithm] Programmers 로또의 최고순위와 최저순위 풀이

Jeong Jeon
반응형

순위를 어떻게 할지만 잘 결정하면 답이 나온 문제였다..

 

class Solution {
    public int[] solution(int[] lottos, int[] win_nums) {
        int[] answer = {};
        
        //0의 갯수를 찾는다.
        //맞는 번호 갯수를 찾는다.
        int zeroCnt = 0;
        int winCnt = 0;
        
        for(int lotto : lottos){
            if(lotto == 0){
                zeroCnt++;
            }else{
                for(int win : win_nums){
                    if(lotto == win){
                        winCnt ++;
                        //체크하면 두번은 없으니 break;
                        break;
                    }
                }
            }
        }
        
        answer = new int[2];
        answer[0] = Math.min(7 - (winCnt + zeroCnt), 6);
        answer[1] = Math.min(7 - winCnt, 6);
        return answer;
    }
}
반응형