[프로그래머스] 이모티콘 할인 행사

최동혁·2023년 5월 1일
0

1일 1코테 이상

목록 보기
4/10

풀이

범위가 얼마 안되는걸 보고 아 브루트 포스구나 하고 바로 itertools 모듈안에 있는 콤비네이션 시리즈를 떠올렸다.
m개의 이모티콘이 있는데, 10, 20, 30, 40 4개 중에 하나씩 할인을 적용해줘야한다.
하지만 중복은 허용하기 때문에 itertools의 product를 사용하면 된다.
cases = list(itertools.product(items, repeat=len(emoticons)))
이런식으로 쓰면, 각 이모티콘에 [10, 20, 30, 40]이 저장되어 있는 items를 골고루 나눠준다.
(10, 10, 10, 10), (10, 10, 10, 20) ... (40, 40, 40, 40) 이런식의 중복 조합 경우가 나온다.
모든 경우를 따져가면서 할인을 적용하되, 각 유저마다 정해놓은 한계치를 넘으면 서비스를 가입시키고, 할인을 다 적용해서 구매했는데 한계치에 달하지 않으면 수익으로 잡는다.
여기서 핵심은 우선순위가 수익이 아닌, 서비스 가입자 수 이다.
그렇기에 모든 경우에 있어서 서비스 가입 수와 수익을 전부 집어넣고, 서비스 가입 수를 우선순위로 정렬하면 된다.

코드

import itertools
def solution(users, emoticons):
    answer = []
    
    items = [10, 20, 30, 40]

    cases = list(itertools.product(items, repeat=len(emoticons)))

    for case in cases:
        cnt = 0
        total = 0
        for sale, limit in users:  
            user_total = 0
            for i in range(len(emoticons)):
                if sale <= case[i]:
                    user_total += emoticons[i] - emoticons[i] * case[i] * 0.01
            if user_total >= limit:
                cnt += 1
            else:
                total += user_total
        answer.append((cnt, total))
    
    answer = sorted(answer, reverse = True, key = lambda x : (x[0], x[1]))
    
    return answer[0]
profile
항상 성장하는 개발자 최동혁입니다.

0개의 댓글