프로그래머스(77484) 로또 최고등수, 최저등수

Siwoo Pak·2021년 12월 22일
0

자료구조&알고리즘

목록 보기
37/38

프로그래머스 77484

배열을 이용해서 풀기(시간복잡도: O(n^2))

// 개수를 구하는 함수
const getCount = (...args) => {
    if(args.length === 2) {
        return args[0].filter( n => args[1].includes(n)).length;
    } else {
        return args[0].filter( n => n === 0).length;
    }
}
function solution(lottos, win_nums) {
    const rank = [6,6,5,4,3,2,1];
    
    const zeroCnt = getCount(lottos);
    if(zeroCnt === 6) return [1,6];
    
    const worst = getCount(lottos, win_nums);
    if(worst === 6) return [1,1];
    
    return [rank[worst+zeroCnt], rank[worst]]
}

set 사용해서 풀어보기(O(n))

function solution(lottos, win_nums) {
    const rank = [6,6,5,4,3,2,1];
    
    const zeroCnt = lottos.filter(n=> !n).length;
    const lottoSet = new Set([...lottos]);
    
    const worst = new Set(win_nums.filter(n => lottoSet.has(n))).size; 
    
    return [rank[worst+zeroCnt], rank[worst]]
}
profile
'하루를 참고 인내하면 열흘을 벌 수 있고 사흘을 참고 견디면 30일을, 30일을 견디면 3년을 벌 수 있다.'

0개의 댓글