programmers 77484 - 로또의 최고 순위와 최저 순위

문제 분석

  • 문제 유형 : 브루트 포스
  • 걸린 시간 : 11분 44초
  • 풀이 전략 : 1 ~ 45까지 방문 배열을 만들고 -> 일치하는 숫자가 있는지 카운트 한다. -> 0인 것은 따로 카운트 한다. -> 0인 숫자까지 모두 일치하는 경우, 모두 불일치하는 경우의 순위를 answer에 각각 채워넣는다.
  • 걸렸던 부분 : 일치하는 숫자가 하나도 없을 때 등 코너케이스 처리가 바로바로 안되었다.

나의 풀이

class Solution {
    public int[] solution(int[] lottos, int[] win_nums) {
        int[] answer = new int[2];
        int correct = 0;
        boolean[] check = new boolean[46];
        int zero = 0;
        
        for (int i = 0; i < lottos.length; i++) {
            check[lottos[i]] = true;
            if (lottos[i] == 0) zero++;
        }
        
        for (int i = 0; i < win_nums.length; i++) {
            if (check[win_nums[i]]) correct++;
        }
        
        answer[0] = 7 - (correct + zero);
        answer[1] = 7 - correct;
        
        if (answer[0] == 7) answer[0] = 6;
        if (answer[1] == 7) answer[1] = 6;
        
        return answer;
    }
}

다른 사람의 풀이

import java.util.Arrays;
import java.util.stream.LongStream;

class Solution {
    public int[] solution(int[] lottos, int[] winNums) {
        return LongStream.of(
                (lottos.length + 1) - Arrays.stream(lottos).filter(l -> Arrays.stream(winNums).anyMatch(w -> w == l) || l == 0).count(),
                (lottos.length + 1) - Arrays.stream(lottos).filter(l -> Arrays.stream(winNums).anyMatch(w -> w == l)).count()
        )
                .mapToInt(op -> (int) (op > 6 ? op - 1 : op))
                .toArray();
    }
}
import java.util.HashMap;
import java.util.Map;

class Solution {
    public int[] solution(int[] lottos, int[] win_nums) {
        Map<Integer, Boolean> map = new HashMap<Integer, Boolean>();
        int zeroCount = 0;

        for(int lotto : lottos) {
            if(lotto == 0) {
                zeroCount++;
                continue;
            }
            map.put(lotto, true);
        }


        int sameCount = 0;
        for(int winNum : win_nums) {
            if(map.containsKey(winNum)) sameCount++;
        }

        int maxRank = 7 - (sameCount + zeroCount);
        int minRank = 7 - sameCount;
        if(maxRank > 6) maxRank = 6;
        if(minRank > 6) minRank = 6;

        return new int[] {maxRank, minRank};
    }
}
class Solution {
    public int[] solution(int[] lottos, int[] win_nums) {
        int[] answer = new int[2];

        int cnt1 = 0;
        int cnt2 = 0;
        for(int i : lottos) {
            if(i == 0) {
                cnt1++;
                continue;
            }
            for(int j : win_nums) {
                if(i == j) cnt2++;
            }
        }


        answer[0] = getGrade(cnt1+cnt2);
        answer[1] = getGrade(cnt2);

        return answer;
    }

    public int getGrade(int n) {
        switch(n) {
            case 6 :
                return 1;
            case 5 :
                return 2;
            case 4 :
                return 3;
            case 3 :
                return 4;
            case 2 :
                return 5;
            default :
                return 6;
        }
    }
}