ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

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

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

 

 

๐Ÿ“  ํ’€์ด

๋งจ ๋ฐ‘๋‹จ์—์„œ๋ถ€ํ„ฐ ์ˆ˜์ต๊ธˆ์„ ์œ„์ชฝ์œผ๋กœ ๋ถ„๋ฐฐํ•ด์ฃผ๋Š” ๋ฐฉ์‹์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ’€์–ด์•ผ ํ•œ๋‹ค. sam์˜ ์ˆ˜์ต๊ธˆ์ค‘ 10%๋ฅผ edward๋กœ ์˜ฌ๋ฆฌ๊ณ  ๋‹ค์‹œ edward์—์„œ ํ•ด๋‹น ์ˆ˜์ต๊ธˆ์„ mary๋กœ ์˜ฌ๋ฆฌ๊ณ  ๋งˆ์ง€๋ง‰์œผ๋กœ mary์—์„œ center๋กœ ์˜ฌ๋ฆฐ๋‹ค.

 

1. ์šฐ์„  ์ˆ˜์ต๊ธˆ์„ ์ €์žฅํ•˜๋Š” memberProfit๊ณผ ๋ˆ„๊ตฌ๋กœ๋ถ€ํ„ฐ ์ถ”์ฒœ์„ ๋ฐ›์•˜๋Š”์ง€ ์ฆ‰, ๋ถ€๋ชจ ๋…ธ๋“œ๋ฅผ ์ €์žฅํ•˜๋Š” memberRecommender๋ฅผ ๋งŒ๋“ ๋‹ค.

memberRecommender = dict() # ์ถ”์ฒœ์ธ dict์— ๋„ฃ๊ณ 
    memberProfit = dict() # ์ˆ˜์ต๊ธˆ
    
    for i in range(len(enroll)):
        memberRecommender[enroll[i]] = referral[i]
        memberProfit[enroll[i]] = 0

2. seller๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ ์ˆ˜์ต๊ธˆ์„ ๋ฐฐ๋ถ„ํ•˜๋Š”๋ฐ ์—ฌ๊ธฐ์„œ DFS ๊ฐœ๋…์„ ์‚ฌ์šฉํ•œ๋‹ค. ๋ถ€๋ชจ ๋…ธ๋“œ๊ฐ€ '-'๊ฐ€ ์•„๋‹ ๋•Œ๊นŒ์ง€ ์ฆ‰, center์ผ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ ๋ถ€๋ชจ๊ฐ€ '-'์ธ ๊ฒฝ์šฐ์—๋„ center๋กœ ์ˆ˜์ต๊ธˆ์„ ์ค˜์•ผํ•˜๋ฏ€๋กœ ์ž์‹ ์˜ ์ˆ˜์ต์—์„œ ๋นผ์ค˜์•ผํ•œ๋‹ค.

for i in range(len(seller)):
        cur = seller[i]
        profit = amount[i]*100
        memberProfit[cur] += amount[i]*100
        
        while memberRecommender[cur] != '-': # ๋ถ€๋ชจ ๋…ธ๋“œ๊ฐ€ center์ด ์•„๋‹ ๋•Œ๊นŒ์ง€
            if profit == 0: # ์ค‘์š”!! 
                break
            profit = int(0.1*profit) 
            memberProfit[cur] -= profit # ํ˜„์žฌ ๋…ธ๋“œ์—์„œ 0.1*์ˆ˜์ต๊ธˆ ๋นผ์ฃผ๊ณ 
            memberProfit[memberRecommender[cur]] += profit # ๋ถ€๋ชจ๋…ธ๋“œ์˜ ์ˆ˜์ต๊ธˆ์— ๋”ํ•ด์ค€๋‹ค
            cur = memberRecommender[cur]   # ๊ทธ๋ฆฌ๊ณ  ๋‹ค์‹œ ๋ถ€๋ชจ๋…ธ๋“œ๋ฅผ ํ˜„์žฌ ๋…ธ๋“œ๋กœ ๊ต์ฒดํ•ด์„œ ๋ฐ˜๋ณต ์ˆ˜ํ–‰ํ•œ๋‹ค
            
        if memberRecommender[cur] == '-': # ๋ถ€๋ชจ๊ฐ€ center์ธ ๊ฒฝ์šฐ 
            memberProfit[cur] -= int(profit*0.1)

์—ฌ๊ธฐ์„œ ์กฐ์‹ฌํ•ด์•ผํ• ๊ฒŒ ์ˆ˜์ต์ด 0์›์ด ๋œ ๊ฒฝ์šฐ ๋”์ด์ƒ ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ฆด ํ•„์š”๊ฐ€ ์—†์œผ๋ฏ€๋กœ ์ข…๋ฃŒํ•ด์ค€๋‹ค. ํ•ด๋‹น ์กฐ๊ฑด๋ฌธ ์—†์œผ๋ฉด ๋งˆ์ง€๋ง‰ 3๊ฐœ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์—์„œ ์‹œ๊ฐ„ ์ดˆ๊ณผ ๋ฐœ์ƒํ•œ๋‹ค.

๐Ÿ–ฅ ์ตœ์ข… ์ฝ”๋“œ

"""
์–ด๋Š์ •๋„ ํŒ๋งค๊ฐ€ ์ด๋ฃจ์–ด์ง„ ํ›„ ๋ˆ„๊ฐ€ ์–ผ๋งˆ๋งŒํผ์˜ ์ด๋“์„ ๊ฐ€์ ธ๊ฐ”๋Š”์ง€
๋ฏผํ˜ธ -> center, 8๋ช…์˜ ํŒ๋งค์› 
๋ชจ๋“  ํŒ๋งค์›์€ ์นซ์†”์˜ ํŒ๋งค์— ์˜ํ•˜์—ฌ ๋ฐœ์ƒํ•˜๋Š” ์ด์ต์—์„œ 10%๋ฅผ ๊ณ„์‚ฐํ•˜์—ฌ ์ž์‹ ์„ ์กฐ์ง์— ์ฐธ์—ฌ์‹œํ‚จ ์ถ”์ฒœ์ธ์—๊ฒŒ ๋ฐฐ๋ถ„ํ•˜๊ณ  ๋‚˜๋จธ์ง€ ๊ฐ€์ง
๋ชจ๋“  ํŒ๋งค์›์€ ์ž์‹ ์ด ์นซ์†”ํŒ๋งค์—์„œ ๋ฐœ์ƒํ•œ ์ด์ต๋ฟ๋งŒ์•„๋‹ˆ๋ผ ๊ฐ€์ž…์‹œํ‚จ ํŒ๋งค์›Œ 10%๊นŒ์ง€ ์ž๊ธฐ ์ด์ต
enroll -> ๋ฏผํ˜ธ๋ฅผ ์ œ์™ธํ•œ ์กฐ์ง์› 
referral
i๋ฒˆ์งธ ์ด๋ฆ„ 
"""

def solution(enroll, referral, seller, amount):
    result = []
    memberRecommender = dict() # ์ถ”์ฒœ์ธ dict์— ๋„ฃ๊ณ 
    memberProfit = dict() # ์ˆ˜์ต๊ธˆ
    
    for i in range(len(enroll)):
        memberRecommender[enroll[i]] = referral[i]
        memberProfit[enroll[i]] = 0
        
    for i in range(len(seller)):
        cur = seller[i]
        profit = amount[i]*100
        memberProfit[cur] += amount[i]*100
        while memberRecommender[cur] != '-':
            if profit == 0:
                break
            profit = int(0.1*profit)
            memberProfit[cur] -= profit
            memberProfit[memberRecommender[cur]] += profit
            cur = memberRecommender[cur]    
        if memberRecommender[cur] == '-':
            memberProfit[cur] -= int(profit*0.1)
    for member in enroll:
        result.append(memberProfit[member])
    return result
๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
Total
Today
Yesterday
๋งํฌ
ยซ   2024/11   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
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
๊ธ€ ๋ณด๊ด€ํ•จ