코테

[Algorithm] 로또의 최고 순위와 최저 순위 - JavaScript

뚜따따 2024. 3. 5. 21:11

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

난이도 : Level 1
정답률: 56%

▶ 문제

▶ 풀이

lottos 를 돌며 0의 개수와 win_nums 와 겹치는 숫자를 파악해 계산한다.

function solution(lottos, win_nums) {
    let score = 0;
    let zeroCount = 0;
    lottos.forEach(x => {
        if(x !== 0) {
        if(win_nums.includes(x)) {
            score++;
        } 
        } else {
            zeroCount++;
        }
    })
    let max = score + zeroCount
    return [max !== 0 ? 7 - max : 6, score !== 0 ? 7 - score : 6];
}

▶ 다른 사람의 풀이

function solution(lottos, win_nums) {
    const rank = [6, 6, 5, 4, 3, 2, 1];

    let minCount = lottos.filter(v => win_nums.includes(v)).length;
    let zeroCount = lottos.filter(v => !v).length;

    const maxCount = minCount + zeroCount;

    return [rank[maxCount], rank[minCount]];
}

rank 배열을 이미 만들어 놓고 사용하는 방법. 가독성이 훨씬 좋아 보인다.