-
-
Save omo/15fa7a67d90a255d2c642b15f6507560 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 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