문제 설명
https://school.programmers.co.kr/learn/courses/30/lessons/77486
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
문제 풀이
- tree 라는 자식노드: 부모노드 딕셔너리를 이용해서 문제 품
- tree에는 center를 따로 기입해서 최종 부모를 넣음
- seller를 순회하면서 이익금을 계산
- seller[i]의 총 판매액(money)은 amount[i] * 100
- 이 때 seller[i]의 추천인이 있다면 money의 90%를 seller[i] 의 이익금(profit) 에 담아주고 10%는 추천인 이익금의 담아줌
- 이때 추천인의 추천인들에게도 해당 이익금(10%) 에 대해서도 tree를 타고 올라가면서 10%를 떼줘야 함.
- 10%를 떼주는거니까 그만큼 현재의 profit에는 10%를 빼줘야함
- 이 과정은 while문으로 수행하고 만약 parent가 center 라면 center에게 10%를 떼주고 끝내야 하고,
- 10원 미만일 경우에는 10%가 0이 되므로 더이상 떼줄 것이 없어서 while문을 탈출해야함
소스 코드
# 판매원들의 각각의 이익금을 enroll 이름이 포함된 순서에 따라 출력
# 판매원 이름, 추천인, 물건 판 판매원 이름, 그 판매원들의 이익
def solution( en, ref, sel, amount):
n = len(en)
answer = [0]*n
tree = dict()
profit = dict()
profit["center"] = 0
tree["center"]="center"
for i in range(n):
profit[en[i]]=0
if ref[i]=='-':
tree[en[i]]='center'
else:
tree[en[i]]=ref[i]
for i in range(len(sel)):
name = sel[i]
parent = tree[name]
money = amount[i]*100
profit[name]+=int(money*0.9)
profit[parent]+=int(money*0.1)
money = int(money*0.01)
while(True):
profit[parent]-=money
parent=tree[parent]
profit[parent]+=money
money=int(money*0.1)
if(money<=0 or parent=="center"):
break
for i in range(n):
answer[i]=profit[en[i]]
return answer
# print(solution(["john", "mary", "edward", "sam", "emily", "jaimie", "tod", "young"],
# ["-", "-", "mary", "edward", "mary", "mary", "jaimie", "edward"],
# ["young", "john", "tod", "emily", "mary"],
# [12, 4, 2, 5, 10]))
#
# ["john", "mary", "edward", "sam", "emily", "jaimie", "tod", "young"]
# [360, 958, 108, 0, 450, 18, 180, 1080]
'코딩 테스트 > 프로그래머스' 카테고리의 다른 글
[링크드리스트] 표 편집 (파이썬) (0) | 2023.05.06 |
---|---|
[파이썬] 풍선 터트리기 (0) | 2023.05.03 |
[탐색] 자물쇠와 열쇠 (파이썬) (0) | 2023.04.25 |
[이분탐색] 입국심사 (파이썬) (0) | 2023.04.22 |
[탐색] 경주로 건설 (파이썬) (0) | 2023.04.22 |