Skip to content

Instantly share code, notes, and snippets.

@omo

omo/Solution.kt Secret

Created August 1, 2022 11:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save omo/1dd017d184079bfbada6479718d73a5c to your computer and use it in GitHub Desktop.
Save omo/1dd017d184079bfbada6479718d73a5c to your computer and use it in GitHub Desktop.
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