PS (Problem Solving)/Programmers

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

캐럿노트 2022. 8. 14. 19:00

문제

https://school.programmers.co.kr/learn/courses/30/lessons/77484#

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

설계

- 맞힌 숫자를 제거 → 지워진 0의 개수와 맞히지 못한 숫자간의 최고로 맞힐 수 있는 경우의 수 확인

- high, low의 최고로 많이 맞힐 수 있는 숫자에 따라 결과를 hash map에서 선택하여 최종 반환

 

python

def solution(lottos, win_nums):
    high, low = 0, 0    # 최대, 최소 맞힐 수 있는 번호
    zero = lottos.count(0)

    # 맞힌 번호 count
    for number in lottos:
        if number in win_nums:
            low += 1
            win_nums.remove(number)

    # 0에 들어갈 수 있는 경우의 수 확인
    if zero <= len(win_nums):
        high = low + zero
    else:
        high = low + len(win_nums)

    # 등수 hash map
    result = {6: 1, 5: 2, 4: 3, 3: 4, 2: 5, 1: 6, 0: 6}
    return [result[high], result[low]]

 

JavaScript

function solution(lottos, win_nums) {
  let minV = 0;
  let maxV = 0;
  let zero = 0;

  lottos.forEach((lotto) => {
    if (win_nums.includes(lotto)) {
      minV++;
    } else if (lotto === 0) {
      zero++;
    }
  });
  maxV = minV + zero;
  
  // 점수 계산
  score = { 6: 1, 5: 2, 4: 3, 3: 4, 2: 5 };
  if (minV >= 2) {
    minV = score[minV];
  } else {
    minV = 6;
  }
  if (maxV >= 2) {
    maxV = score[maxV];
  } else if (maxV > 6) {
    maxV = 1;
  } else {
    maxV = 6;
  }
  const answer = [maxV, minV];
  return answer;
}