Skip to content
Closed
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
53 changes: 53 additions & 0 deletions dynamic_programming/house_robber.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""
Code for house robber problem LeetCode : 198 using dynamic programming (tabulation).
"""

### Question explanation:
# Given array of numbers which says how much money you can steel form houses.
# The only constraint is if you rob from adjacent houses, it will ring alarm to police.

### My Approach with DP:
# As starting with DP, I will explain the approach in recursion with memoization which is more intuitive.

Check failure on line 10 in dynamic_programming/house_robber.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

dynamic_programming/house_robber.py:10:89: E501 Line too long (105 > 88)
# At each node in recursion, we have counter which True or False
# Starting from last house, we have 2 options 1) If choosen, counter=True will be passed in recursion.

Check failure on line 12 in dynamic_programming/house_robber.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

dynamic_programming/house_robber.py:12:89: E501 Line too long (102 > 88)
# 2) If not choosen, counter=False will be passes in recursion.

Check failure on line 13 in dynamic_programming/house_robber.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

dynamic_programming/house_robber.py:13:89: E501 Line too long (107 > 88)
# If choosen=True at current house means previous house is choose so you can't choose current house.

Check failure on line 14 in dynamic_programming/house_robber.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

dynamic_programming/house_robber.py:14:89: E501 Line too long (100 > 88)
# If choosen=False at current house means it is not choosen, it means you have option to choose or not current house.

Check failure on line 15 in dynamic_programming/house_robber.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

dynamic_programming/house_robber.py:15:89: E501 Line too long (117 > 88)
## Example nums=[1,2,3,1]

### Last index value (1, counter=False) (as we are starting from here we are free to choose)

Check failure on line 18 in dynamic_programming/house_robber.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

dynamic_programming/house_robber.py:18:89: E501 Line too long (95 > 88)
# / \
### (3,counter=True) (3,counter=False) (counter=True mean 1 is choosen,so avoid 3)

Check failure on line 20 in dynamic_programming/house_robber.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

dynamic_programming/house_robber.py:20:89: E501 Line too long (101 > 88)
# / / \
### (2,counter=False) (2,True) (2,False)
# (here 2,False means 1 is choosen, so we avoid 3 and
# as 3 is avoided now again we are free to choose at house 2)


## Note: dp[i][0]=>0 is True and 1 is False in convention.
def rob(nums):
def helper(nums, dp, i, j):
if i < 0:
return 0
if dp[i][j] != -1:
return dp[i][j]
if j == True:

Check failure on line 34 in dynamic_programming/house_robber.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E712)

dynamic_programming/house_robber.py:34:12: E712 Avoid equality comparisons to `True`; use `j:` for truth checks
dp[i][0] = helper(nums, dp, i - 1, False)
return dp[i][0]
else:
dp[i][1] = max(
nums[i] + helper(nums, dp, i - 1, True), helper(nums, dp, i - 1, False)
)
return dp[i][1]

dp = [[-1] * 2 for _ in range(0, len(nums))]

Check failure on line 43 in dynamic_programming/house_robber.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (PIE808)

dynamic_programming/house_robber.py:43:35: PIE808 Unnecessary `start` argument in `range`
return helper(nums, dp, len(nums) - 1, False)


if __name__ == "__main__":
nums = [2, 7, 9, 3, 1]
if not nums:
print("The list is empty")
else:
max_amount = rob(nums)
print(max_amount)
Loading