Skip to content

Instantly share code, notes, and snippets.

@luoheng23
Created December 26, 2019 12:53
Show Gist options
  • Save luoheng23/60968696e68dceb6c4a65813a1e6d004 to your computer and use it in GitHub Desktop.
Save luoheng23/60968696e68dceb6c4a65813a1e6d004 to your computer and use it in GitHub Desktop.
func max(x, y int) int {
if x > y {
return x
}
return y
}
func rob(nums []int) int {
if len(nums) == 0 {
return 0
}
if len(nums) == 1 {
return nums[0]
}
if len(nums) == 2 {
return max(nums[0], nums[1])
}
a, b := nums[0], max(nums[0], nums[1])
for i := 2; i < len(nums); i++ {
a, b = b, max(a + nums[i], b)
}
return b
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment