블로그 이름 뭐로 하지

[알고리즘/파이썬] 프로그래머스 - 이모티콘 할인행사 본문

알고리즘

[알고리즘/파이썬] 프로그래머스 - 이모티콘 할인행사

발등이 따뜻한 사람 2024. 1. 18. 22:52

문제

https://school.programmers.co.kr/learn/courses/30/lessons/150368

 

프로그래머스

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

programmers.co.kr

 

풀이

할인율이 10,20,30,40 으로 정해져있다는 말을 못 보고.. ㅠㅠ 그냥 랜덤인줄 알고 풀이가 도저히 떠오르지 않았고... 이게 정말 레벨2가 맞는건지 .. 의심이 갔다. 근데 할인율이 정해져있는 문제였다.ㅎ....

가능한 할인율 조합을 모두 구해서 그 조합따라서 가입자수와 이익을 계산해서 가장 좋은 경우에 값을 저장해두어 출력해주면 된다.

 

코드

def solution(users, emoticons):
    answer = [0, 0]
    sale_rate = [10, 20, 30, 40]
    sale = []

    def dfs(temp, depth):
    
        if depth == len(temp):
            sale.append(temp[:])
            return
            
        for s in sale_rate:
            temp[depth] += s
            dfs(temp, depth + 1)
            temp[depth] -= s

    dfs([0] * len(emoticons), 0)

    for d in range(len(sale)):
        user_count = 0
        profit = 0

        for user in users:
            buy_count = 0
            for i in range(len(emoticons)):
                if sale[d][i] >= user[0]:
                    buy_count += emoticons[i] * ((100 - sale[d][i]) / 100)
            if user[1] <= buy_count:
                user_count += 1
            else:
                profit += buy_count

        if answer[0] < user_count:
            answer = [user_count, int(profit)]
        elif answer[0] == user_count:
            if answer[1] < profit:
                answer = [user_count, int(profit)]

    return answer