[LeetCode] 746. Min Cost Climbing Stairs(Kotlin)

0

LeetCode

목록 보기
33/58
post-thumbnail

[LeetCode] 746. Min Cost Climbing Stairs(Kotlin)

풀이

class Solution {
    private lateinit var costList:List<Int> 
    private val memoization = MutableList<Int>(1000){-1}

    private fun getMinCost(index:Int):Int{
        if(index == costList.size) return 0

        if(memoization[index] != -1) 
            return memoization[index]
        
        var minCost = Int.MAX_VALUE
        if(index+1 <= costList.size) 
            minCost = min(minCost, costList[index] + getMinCost(index+1))
        if(index+2 <= costList.size) 
            minCost = min(minCost, costList[index] + getMinCost(index+2))
        
        memoization[index] = minCost
        return minCost
    }

    fun minCostClimbingStairs(cost: IntArray): Int {
        costList = cost.toList()

        return min(getMinCost(0), getMinCost(1))
    }
}
profile
Be able to be vulnerable, in search of truth

0개의 댓글