g0701_0800.s0746_min_cost_climbing_stairs.Solution.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-kotlin Show documentation
Show all versions of leetcode-in-kotlin Show documentation
Kotlin-based LeetCode algorithm problem solutions, regularly updated
package g0701_0800.s0746_min_cost_climbing_stairs
// #Easy #Array #Dynamic_Programming #Dynamic_Programming_I_Day_2
// #Level_1_Day_11_Dynamic_Programming #2023_03_04_Time_171_ms_(96.76%)_Space_36_MB_(78.14%)
class Solution {
fun minCostClimbingStairs(cost: IntArray): Int {
val dp = IntArray(cost.size)
dp[0] = cost[0]
dp[1] = cost[1]
for (i in 2 until cost.size) {
dp[i] = cost[i] + dp[i - 1].coerceAtMost(dp[i - 2])
}
return dp[cost.size - 1].coerceAtMost(dp[cost.size - 2])
}
}