1일 1PS 30일차!

📚 문제


Lv 2

  • 할인은 4 종류 뿐이며 이모티콘의 종류는 최대 7종류 뿐이다.
    • 이모티콘 할인 총 경우의 수는 4^7 이므로 충분히 완전 탐색이 가능하다.

💡 포인트


  1. 모든 이모티콘을 할인률을 적용한 값들을 계산한다.
  2. 이모티콘의 수를 m 이라 했을 때 4^m 번 반복하며 모든 경우에 대해서 계산한다.

💻 코드



def solution(users, emoticons):
    discount = [0.6, 0.7, 0.8, 0.9]
    m = len(emoticons)
    emoticon_discount = [[0 for _ in range(4)] for _ in range(m)]
    for i in range(m):
        for j in range(4):
            emoticon_discount[i][j] = emoticons[i] * discount[j]

    max_plus_user = 0
    max_total_cost = 0
    for i in range(4**m):
        plus_user = 0
        total_cost = 0
        combination = [(i // (4**j)) % 4 for j in reversed(range(m))]
        for user in users:
            cost = 0
            if user[0] <= 10:
                for index, com in enumerate(combination):
                    if com <= 3:
                        cost += emoticon_discount[index][com]
            elif user[0] <= 20:
                for index, com in enumerate(combination):
                    if com <= 2:
                        cost += emoticon_discount[index][com]
            elif user[0] <= 30:
                for index, com in enumerate(combination):
                    if com <= 1:
                        cost += emoticon_discount[index][com]
            elif user[0] <= 40:
                for index, com in enumerate(combination):
                    if com <= 0:
                        cost += emoticon_discount[index][com]

            if cost >= user[1]:
                plus_user += 1
            else:
                total_cost += cost

        if max_plus_user < plus_user:
            max_plus_user = plus_user
            max_total_cost = total_cost
        elif max_plus_user == plus_user:
            max_total_cost = max(max_total_cost, total_cost)

    answer = [max_plus_user, int(max_total_cost)]
    return answer


업데이트:

댓글남기기