728x90
https://school.programmers.co.kr/learn/courses/30/lessons/150368
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
📄 문제개요
- 해당문제는 유저와 이모니콘이 주어졌을때 조건에따라서 가입한 유저와 이모티콘을 산 가격을 나누어서 우선순위를 매겨, 가장 좋은 우선순위의 값을 출력하는 문제이다.
- 이모티콘은 10%, 20%, 30% 40% 할인 할 수 있다.
- 유저는 유저가 설정한 할인율 이상만큼 할인 하면 이모티콘을 산다.
- 모든 이모티콘을 산가격과 유저가 설정한 이모티콘 가격의 합보다 클경우 유저는 이모티콘 플러스 서비스에 가입한다.
- 그렇지 않다면 유저는 이모티콘을 모두 구매한다.
🤔 문제분석
- 이모티콘과 할인율을 조합하여 가능한 모든 경우의 수를 만든다.
- 모든 유저는 가능한 모든 경우의 수를 계산해보고 조건에 맞는 최대 수치를 업데이트 한다.
- 모든 순회가 끝나면 최대 수치를 출력한다.
📝 의사코드
- 이모티콘과 할인율을 조합하여 가능한 모든경우의 수를 만든다.
- 모든 유저들이 만든 조합의 이모티콘을 사보면서 서비스에 가입할 사람, 이모티콘을 산 가격을 계산한다.
- 계산한 값을 현재 값과 비교하여 갱신한다.
💻 코드
from itertools import product
discounts = [10, 20, 30, 40]
def solution(users, emoticons):
best_combo = max(emotion_discount(users, emoticons), key=lambda x: (x[0], x[1]))
return [best_combo[0], best_combo[1]]
def emotion_discount(users, emoticons):
results = []
for discount_combo in product(discounts, repeat=len(emoticons)):
reg_cnt, total_charge = calculate_user_responses(users, emoticons, discount_combo)
results.append((reg_cnt, total_charge))
return results
def calculate_user_responses(users, emoticons, discount_combo):
reg_cnt = 0
total_charge = 0
for user in users:
discount_rate, max_money = user
charge = sum(emo * (100 - discount) / 100 for emo, discount in zip(emoticons, discount_combo) if discount >= discount_rate)
if charge >= max_money:
reg_cnt += 1
else:
total_charge += charge
return reg_cnt, total_charge
🎯 피드백 및 개선사항
- 주어진 조건을 천천히 생각해보면 쉽게 풀 수 있는 문제이다.
❓다른사람은 어떻게 풀었을까?
- 구현 문제로 패스하겠습니다.
728x90
'알고리즘 > 프로그래머스' 카테고리의 다른 글
[프로그래머스] 퍼즐조각 채우기 (0) | 2024.04.10 |
---|---|
[프로그래머스] 2023 Kakao Blind Recruitment : 표병합 (0) | 2023.11.22 |
[프로그래머스] 2023 Kakao Blind Recuiment : 표현 가능한 이진트리 (0) | 2023.11.21 |
[프로그래머스] 2023 Kakao Blind Recuiment 택배 배달 수거하기 (1) | 2023.11.20 |
[프로그래머스] 2020 Kakao Blind Recuritment : 가사 검색 (1) | 2023.11.18 |