Programmers/1단계

[2021 Dev-Matching: 웹 백엔드 개발] 로또의 최고 순위와 최저 순위

Snowboarder 2022. 9. 29. 11:26
 

프로그래머스

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

programmers.co.kr

문제풀이

def solution(lottos, win_nums):
    answer = []
    cnt = lottos.count(0)



    if lottos == win_nums:
        best_rank = 1
        worst_rank = 6

    else:
        lottos2 = set(lottos)
        with_nums2 = set(win_nums)
        correct =len(lottos2 & with_nums2)
        best= correct + cnt
        worst = correct

        best_rank = len(lottos)- best +1
        worst_rank = len(lottos) - worst +1

    if worst_rank >= 6 :
        worst_rank =6

    if correct == 0 and cnt != len(lottos):
        best_rank = 6

    answer.append(best_rank)
    answer.append(worst_rank)

    return answer

다른사람풀이

def solution(lottos, win_nums):
    rank = {
        0: 6,
        1: 6,
        2: 5,
        3: 4,
        4: 3,
        5: 2,
        6: 1
    }
    return [rank[len(set(lottos) & set(win_nums)) + lottos.count(0)], rank[len(set(lottos) & set(win_nums))]]

문제해석

경우의수

  • 다 같을경우 : best_rank = 1 , worst_rank = 6 으로 초기화
  • 다른 경우 :
    • 집합을 이용해서 (set)교집합
    • 맞는 개수를 구함
    • best_rank, worst_rank를 구할 수 있다.
    • 다 다른 경우
      • best_rank 를 6으로 초기화

총정리

  • 배열을 교집합으로 이용하는거가 핵심이였다,
  • 경우의 수를 잘 두어서 잘 풀었다.
  • 예외의 상황 다다른 경우를 생각을 못해서 조금 시간이 걸렸다.
  • 등수는 조건에 따라 배열로 생각을 안해본게 조금 아쉽다.
  • 효율성은 정말 안좋을 것 같지만 경우의수를 잘 따진게 근본 적인 코드 이지않나 해석하기 쉬울 것 같다.