본문 바로가기
알고리즘/소스코드

PROGRAMMERS 77486: 다단계 칫솔 판매

by cjw.git 2021. 12. 31.

Link : https://programmers.co.kr/learn/courses/30/lessons/77486


Python

더보기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class Node:
    def __init__(self, name):
        self.name = name
        self.amount = 0
        self.parent = None
 
    def __str__(self):
        return self.name + " : " + str(self.amount)
 
 
def traceNode(person: Node, amount):
    if amount < 1:  # 가격이 1원보다 작으면 분배 할 돈이 없으므로 바로 종료
        return
    if person.name == "center":  # 부모라면
        person.amount += amount  # 부모에게 돈을 전부 주고 종료
    else:
        my_amount = amount - (amount // 10)  # 자기 수익금 계산
        person.amount += my_amount  # 자기 수익금을 더해줌
 
        traceNode(person.parent, amount - my_amount)  # 자기 수익금을 뺀 부모를 다시 추적
 
 
def solution(enroll, referral, seller, amount):
    people = dict()
 
    people['center'= Node('center')
    for name in enroll:
        people[name] = Node(name)  # 미리 사람들을 정의하고 dict에 바로 찾을 수 있게 넣어줌
 
    for child, parent in zip(enroll, referral): # (enroll[0], referral[0]) ... (enroll[n], referral[n]) 이 됨
        if parent == "-":  # 만약 부모가 없다면
            people[child].parent = people["center"]  # 민호에게 연결
        else:
            people[child].parent = people[parent]  # 부모에게 연결
 
    for seller, amount in zip(seller, amount):
        amount *= 100  # 칫솔 이익금은 한개당 100원
        traceNode(people[seller], amount)  # 부모를 추적하여 수익을 분배함
    return [people[person].amount for person in enroll]  # enroll 순서대로 수익금을 반환
cs

FeedBack

  1.  

 

 

 

cjw.git@gmail.com

'알고리즘 > 소스코드' 카테고리의 다른 글

PROGRAMMERS 64065: 튜플  (0) 2021.12.31
PROGRAMMERS 72411: 메뉴 리뉴얼  (0) 2021.12.31
LEETCODE 287: Find the Duplicate Number  (0) 2021.08.05
LEETCODE 128. Longest Consecutive Sequence  (0) 2021.08.05
LEETCODE 46: PERMUTATIONS  (0) 2021.08.05

댓글