Skip to content

Instantly share code, notes, and snippets.

@omo

omo/Solution.kt Secret

Created August 1, 2022 12:11
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/15fa7a67d90a255d2c642b15f6507560 to your computer and use it in GitHub Desktop.
Save omo/15fa7a67d90a255d2c642b15f6507560 to your computer and use it in GitHub Desktop.
class Solution {
fun rob(nums: IntArray): Int {
val nhouses = nums.size
val cache = HashMap<Int, Int>()
fun search(h: Int) : Int {
if (h >= nhouses) { return 0 } // No more house
if (!cache.contains(h)) {
cache[h] = nums[h] + (((h + 2) until nhouses).map { search(it) }.max() ?: 0)
}
return cache[h]!!
}
return (0 until nhouses).map { search(it) }.max()!!
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment