From db5f2027439ac6d538ba7e367c40bccc0652164a Mon Sep 17 00:00:00 2001 From: "kota@2004" Date: Fri, 10 Oct 2025 16:44:11 +0530 Subject: [PATCH 1/2] Added house_robber.py --- dynamic_programming/house_robber.py | 52 +++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 dynamic_programming/house_robber.py diff --git a/dynamic_programming/house_robber.py b/dynamic_programming/house_robber.py new file mode 100644 index 000000000000..2cea3a79acd3 --- /dev/null +++ b/dynamic_programming/house_robber.py @@ -0,0 +1,52 @@ +""" +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. +# 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. +# 2) If not choosen, counter=False will be passes in recursion. +# If choosen=True at current house means previous house is choose so you can't choose current house. +# If choosen=False at current house means it is not choosen, it means you have option to choose or not current house. +## Example nums=[1,2,3,1] + +### Last index value (1, counter=False) (as we are starting from here we are free to choose) +# / \ +### (3,counter=True) (3,counter=False) (counter=True mean 1 is choosen,so avoid 3) +# / / \ +### (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: + 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))] + 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) + + + From d0163efa8f4b63d53347422f541ec9084f69f827 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 10 Oct 2025 11:33:04 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dynamic_programming/house_robber.py | 35 +++++++++++++++-------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/dynamic_programming/house_robber.py b/dynamic_programming/house_robber.py index 2cea3a79acd3..66cf2cbff401 100644 --- a/dynamic_programming/house_robber.py +++ b/dynamic_programming/house_robber.py @@ -20,33 +20,34 @@ ### (3,counter=True) (3,counter=False) (counter=True mean 1 is choosen,so avoid 3) # / / \ ### (2,counter=False) (2,True) (2,False) -# (here 2,False means 1 is choosen, so we avoid 3 and +# (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: + def helper(nums, dp, i, j): + if i < 0: return 0 - if dp[i][j]!=-1: + if dp[i][j] != -1: return dp[i][j] - if j==True: - dp[i][0]=helper(nums,dp,i-1,False) + if j == True: + 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)) + 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))] - return helper(nums,dp,len(nums)-1,False) - + + dp = [[-1] * 2 for _ in range(0, len(nums))] + 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') + nums = [2, 7, 9, 3, 1] + if not nums: + print("The list is empty") else: - max_amount=rob(nums) + max_amount = rob(nums) print(max_amount) - - -