알고리즘

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

계양 꿀주먹 2024. 5. 2. 11:20

 


 

import java.util.*;

class Solution {
    public int[] solution(int[] lottos, int[] win_nums) {
        int[] answer = new int[2];
        int zero = 0, cnt = 0; // 0의 갯수, 맞춘 갯수
        
        for(int i = 0; i < lottos.length; i++) {
            if(lottos[i] == 0) {
                zero++;
                continue;
            } else {
                for(int j = 0; j < win_nums.length; j++) {
                    if(lottos[i] == win_nums[j]) {
                        cnt++;
                        break;
                    }
                }
            }        
        }
        answer[0] = (zero == 0 && cnt == 0) ? 6 : 7 - cnt - zero;   // 최고 순위
        answer[1] = (cnt == 0) ? 6 : 7 - cnt;                       // 최저 순위
        
        return answer;
    }
}

 

0의 갯수와 맞춘 갯수를 구해 최고 순위와 최저 순위를 구하는 문제입니다.

하나도 맞추지 못 한거나 하나만 맞췄을 때 6위인 것만 예외로 구하면 어렵지 않은 문제였습니다.