Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions dynamic_programming/house_robber.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""
You are a professional robber planning to rob houses along a street.
Each house has a certain amount of money stashed,
the only constraint stopping you from robbing each of them is that
adjacent houses have security systems connected
and it will automatically contact the police if two adjacent
houses were broken into on the same night.

Given an integer array nums representing the amount of money of each house, we have to
return the maximum amount of money you can rob tonight without alerting the police.

Source : https://leetcode.com/problems/house-robber?envType=problem-list-v2&envId=dynamic-programming


Example

>>> rob([1,2,3,1])
4

"""


def rob(nums: list[int]) -> int:
n = len(nums)
if n < 3:
return max(nums)
dp = [0] * n
dp[0] = nums[0]
dp[1] = max(nums[0], nums[1])
for i in range(2, n):
dp[i] = max(nums[i] + dp[i - 2], dp[i - 1])
return max(dp)


if __name__ == "__main__":
import doctest

doctest.testmod()