Last active
January 6, 2021 04:41
-
-
Save nma/d87c704481591379fb980d70113045ac to your computer and use it in GitHub Desktop.
This file contains hidden or 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
def rob_houses_dp(house: Tuple[int]) -> int: | |
if len(house) == 0: return 0 | |
if len(house) == 1: return house[0] | |
dp = [-1] * len(house) # [-1, -1, -1, -1] default to undesirable value | |
# value of the first house | |
dp[0] = house[0] | |
for i in len(1, len(house)): | |
dp[i] = max( | |
# value of robbing the last house | |
dp[i - 1], | |
# value of robbing current house and house before last | |
house[i] + (dp[i - 2] if i > 1 else 0) | |
) | |
# return last calculated max value | |
return dp[-1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment