[algorithm] 로또의 최고 순위와 최저 순위

yeon·2021년 5월 28일
0

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

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

public static int[] solution(int[] lottos, int[] win_nums) {
        int[] answer = new int[2];

        // lottos의 원소가 모두 0이면 answer = {1,6}
        // lottos에서 0의 개수 파악
        // 0인 아닌 요소들의 match 여부 파악

        Arrays.sort(lottos);
        Arrays.sort(win_nums);
        int match = 0;
        int numOfZero = 0;
        for (int i = 0; i < lottos.length; i++) {
            if (lottos[i] == 0) {
                numOfZero++;
                continue;
            }
            for (int j = 0; j < win_nums.length; j++) {
                if (lottos[i] == win_nums[j]) {
                    match++;
                    j = win_nums.length - 1;
                }
            }
        }

        if (numOfZero == 6) {
            answer[0] = 1;
            answer[1] = 6;
        } else{
            // 최고 순위
            answer[0] = 7 - (match + numOfZero);
            // 최저 순위
            answer[1] = 7 - match;
        }

        if(numOfZero == 0 && match == 0) {
            answer[0] = 6;
            answer[1] = 6;
        }

        return answer;
}

0개의 댓글