๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“– Problem Solution/Programmers

[Swift] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋‹ค๋‹จ๊ณ„ ์นซ์†” ํŒ๋งค

by Fomagran ๐Ÿ’ป 2021. 9. 12.
728x90
๋ฐ˜์‘ํ˜•

 

Problem

 

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๋‹ค๋‹จ๊ณ„ ์นซ์†” ํŒ๋งค

๋ฏผํ˜ธ๋Š” ๋‹ค๋‹จ๊ณ„ ์กฐ์ง์„ ์ด์šฉํ•˜์—ฌ ์นซ์†”์„ ํŒ๋งคํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ํŒ๋งค์›์ด ์นซ์†”์„ ํŒ๋งคํ•˜๋ฉด ๊ทธ ์ด์ต์ด ํ”ผ๋ผ๋ฏธ๋“œ ์กฐ์ง์„ ํƒ€๊ณ  ์กฐ๊ธˆ์”ฉ ๋ถ„๋ฐฐ๋˜๋Š” ํ˜•ํƒœ์˜ ํŒ๋งค๋ง์ž…๋‹ˆ๋‹ค. ์–ด๋Š์ •๋„ ํŒ๋งค๊ฐ€ ์ด๋ฃจ์–ด์ง„ ํ›„,

programmers.co.kr


Solution

 

1. ํŒ๋งค์ž์˜ ๋ถ€๋ชจ์™€ ์ด์ต์„ ์ดˆ๊ธฐํ™”ํ•ด์ค€๋‹ค.

 

    var parentDic:[String:String] = [:]
    var profitDic:[String:Int] = [:]
    
    for i in 0..<enroll.count {
        profitDic[enroll[i]] = 0
        parentDic[enroll[i]] = referral[i]
    }

 

2. ๋ฃจํŠธ ๋ถ€๋ชจ๊ฐ€ ๋‚˜์˜ฌ ๋•Œ๊นŒ์ง€ ์ด์ต์—์„œ 10%๋ฅผ ์ฐจ๊ฐํ•œ๋‹ค.

 

์—ฌ๊ธฐ์„œ ์ฃผ์˜ํ•ด์•ผ ํ•  ์ ์€ n/10์„ ํ–ˆ์„ ๋•Œ 0์ธ ๊ฒฝ์šฐ์—” ๋” ์ด์ƒ ์ง„ํ–‰ํ•ด์ฃผ๋ฉด ์•ˆ๋ฉ๋‹ˆ๋‹ค.(์•ˆ๊ทธ๋Ÿผ ์‹œ๊ฐ„์ดˆ๊ณผ๋‚จ)

 

๊ทธ๋Ÿฌ๋ฏ€๋กœ while๋ฌธ์˜ ์กฐ๊ฑด์„ ๊ฐ€์žฅ ์ตœ์ƒ๋ถ€๋ชจ์ธ "-" ์ด๊ฑฐ๋‚˜ n/10์ด 0๋ณด๋‹ค ํด ๋•Œ๋กœ ๊ฑธ์–ด์ค๋‹ˆ๋‹ค.

 

for i in 0..<seller.count {
        var tenPercent:Int = amount[i]*100
        var parent:String = seller[i]
        while parent != "-" && tenPercent > 0 {
            profitDic[parent]! += tenPercent - tenPercent/10
            parent = parentDic[parent]!
            tenPercent = tenPercent/10
        }
    }

 

3. ์ด์ต์„ enroll์ˆœ์œผ๋กœ ๋ฐ˜ํ™˜ํ•ด์ค€๋‹ค.

 

return enroll.map{profitDic[$0]!}

Source Code

 

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€