[프로그래머스 lev1/JS] 로또의 최고 순위와 최저 순위

woolee의 기록보관소·2022년 10월 27일
0

알고리즘 문제풀이

목록 보기
24/178

문제 출처

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

문제

나의 풀이

비교 대상을 변수로 설정해놓으면 비교하기가 편하다.
const rank = [6,5,4,3,2,1,0];

function solution(lottos, win_nums) {
  let answer=[];
  let min=max=0;
  for (let i=0; i<lottos.length; i++) {
    if (win_nums.includes(lottos[i])) min++;
    if (lottos[i]===0) max++; 
  }
  max += min; 
  
  const rank = [6, 5, 4, 3, 2, 1, 0];

  if (rank.indexOf(min) >= 5) answer[1] = 6;  
  else answer[1] = rank.indexOf(min)+1; 

  if (rank.indexOf(max) >= 5) answer[0] = 6;  
  else answer[0] = rank.indexOf(max)+1; 

  return answer; 
}

console.log(solution([44, 1, 0, 0, 31, 25], [31, 10, 45, 1, 6, 19]));

다른 풀이

function solution(lottos, win_nums) {
    const answer = [];
    const min = lottos.filter(n => win_nums.includes(n)).length;
    const max = lottos.filter(n => n === 0).length + min;

    max > 1 ? answer.push(7 - max) : answer.push(6);
    min > 1 ? answer.push(7 - min) : answer.push(6);

    return answer;
}
profile
https://medium.com/@wooleejaan

0개의 댓글