1 분 소요

사용 언어: Python3

문제

이모티콘 할인행사

풀이

첫 번째

# 1번 목표가 우선이며, 2번 목표가 그 다음입니다.
# 10%, 20%, 30%, 40% 중 하나로 설정
from itertools import product

def solution(users, emoticons):
    answer = []
    discount = [10, 20, 30, 40]
    
    discount_emoticons = []
    for x in product(discount, repeat=len(emoticons)): # 중복순열
        tmp = []
        # print(x)
        for idx, emoticon in enumerate(emoticons):
            tmp.append(emoticon * ((100 - x[idx]) / 100))
        discount_emoticons.append(tmp)
            
    ans = [] # (서비스 가입, 매출액)
    for price in discount_emoticons:
        for user in users:
            cnt, tot = 0
            if sum(price) >= user[1]:
                cnt += 1
                # 서비스 가입
            else:
                tot += sum(price)
                # 매출액 증가
                
                
                
                
    # 행사 목적을 최대한으로 달성했을 때의 이모티콘 플러스 서비스 가입 수와 이모티콘 매출액
            
        
    
    
    return answer
  • 풀다가 포기..

두 번째

discounts = [10,20,30,40]
ans = [-1,-1]

def solution(users, emoticons):
    N, M = len(users), len(emoticons)
    discount_list = [0]*M
    
    def search(idx):
        global ans
        if idx == M:
            sale_num, cost_num = 0, 0 # 서비스 가입자 수, 매출액
            for i in range(N):
                tmp = 0 # 이모티콘 구매비용 (할인률 적용)
                for j in range(M):
                    if users[i][0] <= discount_list[j]:
                        tmp += emoticons[j] * (100-discount_list[j]) // 100
                if tmp >= users[i][1]:
                    sale_num += 1 # 가입자 수 증가
                else:
                    cost_num += tmp # 매출액 증가
            if sale_num > ans[0] or sale_num == ans[0] and cost_num > ans[1]:
                ans = [sale_num, cost_num]
            return
        for i in range(4):
            discount_list[idx] = discounts[i]
            search(idx+1)
    
    search(0)
    return ans

세 번째

def solution(users, emoticons):
    answer = [0, 0]
    data = [10 ,20, 30, 40]
    discount = []
    
    # 이모티콘 할인율 구하기
    def dfs(temp, depth):
        if depth == len(temp):
            discount.append(temp[:])
            return
        for d in data:
            temp[depth] += d
            dfs(temp, depth + 1)
            temp[depth] -= d
    
    dfs([0] * len(emoticons), 0)
    
    # 완전탐색
    for d in range(len(discount)):
        join, price = 0, [0] * len(users)
        for e in range(len(emoticons)):
            for u in range(len(users)):
                # 할인율을 만족하면 구매
                if users[u][0] <= discount[d][e]:
                    price[u] += emoticons[e] * (100 - discount[d][e]) / 100
        
        # 구매 금액에 따라 가입자 갱신
        for u in range(len(users)):
            if price[u] >= users[u][1]:
                join += 1
                price[u] = 0
        
        # 최대 가입자, 구매 금액 갱신
        if join >= answer[0]:
            if join == answer[0]:
                answer[1] = max(answer[1], sum(price))
            else:
                answer[1] = sum(price)
            answer[0] = join
    
    return answer


💛 개인 공부 기록용 블로그입니다. 👻

맨 위로 이동하기