프로그래머스 Lv.1 : 로또의 최고 순위와 최저 순위

zeroequaltwo·2022년 11월 29일
0

코딩테스트

목록 보기
47/69

문제

프로그래머스 문제

내 풀이

function solution(lottos, win_nums) {
    const ranks = [6, 6, 5, 4, 3, 2, 1];
    const fixedCnt = lottos.reduce((acc, num, idx) => num > 0 && win_nums.includes(num) ? acc + 1 : acc, 0);
    const zeroCnt = lottos.reduce((acc, num, idx) => num === 0 ? acc + 1 : acc, 0);
    
    return [ranks[fixedCnt + zeroCnt], ranks[fixedCnt]];
}

개선점

  • reduce를 이용해서 배열 안의 값들을 루프 두번 안 돌리고 계산한 방법이 신기하다.
function solution(lottos = [], win_nums = []) {
  const rank = [6, 6, 5, 4, 3, 2, 1]
  let [max, min] = lottos.reduce(([max, min], cur) => win_nums.includes(cur) ? [++max, ++min] : (cur === 0 ? [++max, min] : [max, min]), [0, 0])
  return [rank[max], rank[min]];
};
profile
나로 인해 0=2가 성립한다.

0개의 댓글