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

버건디·2023년 4월 21일
0

프로그래머스

목록 보기
40/66

문제 링크

- 내 풀이

function solution(lottos, win_nums) {
  let hash = new Map();
  hash.set(0, 6);
  hash.set(1, 6);
  hash.set(2, 5);
  hash.set(3, 4);
  hash.set(4, 3);
  hash.set(5, 2);
  hash.set(6, 1);

  let 이미맞춘로또번호 = lottos.filter((num) => win_nums.includes(num)) // [];

  let 최소맞춘로또번호길이 = 이미맞춘로또번호.length; // 0

  let 낙서된로또 = lottos.filter((num) => num === 0); // 6

  let 낙서된로또길이 = 낙서된로또.length; 6

  let maxRank = hash.get(최소맞춘로또번호길이 + 낙서된로또길이);
  let minRank = hash.get(최소맞춘로또번호길이);

  return [maxRank, minRank];
}

- 다른 사람 풀이

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]];
}

0 을 논리연산자로 적용하면 false이므로, 이 점을 이용한 풀이, 그리고 아예 rank배열을 만들어줬는데, hash보다 간단했다.

profile
https://brgndy.me/ 로 옮기는 중입니다 :)

0개의 댓글

관련 채용 정보