Skip to content

Instantly share code, notes, and snippets.

@nma
Last active January 6, 2021 04:41
Show Gist options
  • Save nma/d87c704481591379fb980d70113045ac to your computer and use it in GitHub Desktop.
Save nma/d87c704481591379fb980d70113045ac to your computer and use it in GitHub Desktop.
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