-
-
Save omo/1dd017d184079bfbada6479718d73a5c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
fun minCostClimbingStairs(cost: IntArray): Int { | |
val nfloors = cost.size | |
val cache = HashMap<Int, Int>() | |
fun search(f: Int) : Int { | |
if (f >= nfloors) { return 0; } // You've reached the top. | |
if (!cache.contains(f)) { | |
cache[f] = cost[f] +arrayOf(search(f + 1), search(f + 2)).min()!! | |
} | |
return cache[f]!! | |
} | |
return if (nfloors == 1) { | |
search(0) | |
} else { | |
arrayOf(search(0), search(1)).min()!! | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment