[프로그래머스] 2021 Dev-Matching: 웹 백앤드 개발자(상반기) > 행렬 테두리 회전하기 (77486)(Kotlin)

원본 문제 보기

문제 설명

문제 설명 보기

[입출력 예]

enroll referral seller amount result
["john", "mary", "edward", "sam", "emily", "jaimie", "tod", "young"] ["-", "-", "mary", "edward", "mary", "mary", "jaimie", "edward"] ["young", "john", "tod", "emily", "mary"] [12, 4, 2, 5, 10] [360, 958, 108, 0, 450, 18, 180, 1080]
["john", "mary", "edward", "sam", "emily", "jaimie", "tod", "young"] ["-", "-", "mary", "edward", "mary", "mary", "jaimie", "edward"] ["sam", "emily", "jaimie", "edward"] [2, 3, 5, 4] [0, 110, 378, 180, 270, 450, 0, 0]

문제 풀이1

풀이 참고

class Solution {
    class Person {
        var name: String = ""
        var parent: Person? = null
        var profit: Int = 0

        constructor(name: String, parent: Person?, profit: Int) {
            this.name = name
            this.parent = parent
            this.profit = profit
        }

        fun calcProfit(profit: Int) {
            val profitToParent = profit / 10
            this.profit += profit - profitToParent
            if (profitToParent >= 1) {
                this.parent?.calcProfit(profitToParent)
            }
        }
    }

    fun solution(enroll: Array<String>, referral: Array<String>, seller: Array<String>, amount: IntArray): IntArray {
        val personMap = mutableMapOf<String, Person>()
        for (name in enroll) {
            personMap[name] = Person(name, null, 0)
        }

        for (i in enroll.indices) {
            if (referral[i] == "-") continue
            personMap[enroll[i]]?.parent = personMap[referral[i]]
        }

        for (i in seller.indices) {
            personMap[seller[i]]?.calcProfit(amount[i] * 100)
        }

        return IntArray(enroll.size) { personMap[enroll[it]]!!.profit }
    }
}