[프로그래머스] 주사위 게임 3

janjanee·2023년 8월 17일
0

코딩테스트

목록 보기
5/6

[프로그래머스] 주사위 게임 3

내 풀이

def solution(a, b, c, d):
    if a == b == c == d:
        return 1111 * a
    
    dice_list = [a, b, c, d]
    dice_set = set(dice_list)
    
    for number in dice_set:
        dice_list.remove(number)
        
    if len(dice_list) == 1:
        dice_set.remove(dice_list[0])
        return dice_set.pop() * dice_set.pop()

    if len(dice_list) == 2:
        if dice_list[0] == dice_list[1]:
            dice_set.remove(dice_list[0])
            return (10 * dice_list[0] + dice_set.pop()) ** 2
        return (dice_list[0] + dice_list[1]) * abs(dice_list[0] - dice_list[1])

    return min(a, b, c, d)

1) 모두 같은 경우는 early return
2) set을 만들어서 set을 순회하며 list의 요소를 제거한다.
3) list의 길이가 1인 경우는 주사위가 2/1/1 의 경우니까 list에 남아있는 마지막 요소를 set에서 삭제하면 남은 1/1 의 두 개의 숫자를 얻을 수 있다.
4) list의 길이가 2인 경우는 두 가지로 나눠진다.

  • 2개의 요소가 같을 경우
    남은 요소가 같다는건 주사위의 숫자가 3/1 인 경우라 남은 요소를 set에서 제거하면
    set에 1개(q) 인 요소의 주사위만 남는다.
  • 2개의 요소가 다를 경우
    남은 요소가 다르다는건 주사위의 숫자가 2/2 인 경우라 그대로 계산한다.

5) 마지막엔 1/1/1/1 로 모두 다른 경우라 min을 써서 최솟값을 구한다.

profile
얍얍 개발 펀치

0개의 댓글