Created
December 26, 2019 12:53
-
-
Save luoheng23/60968696e68dceb6c4a65813a1e6d004 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
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