From 334c37e1466f43d538519dc8e1877a267df1eaeb Mon Sep 17 00:00:00 2001 From: megha-66 Date: Mon, 13 Oct 2025 14:34:24 +0000 Subject: [PATCH 1/4] New DP Algo added --- dynamic_programming/house_robber.py | 38 +++++++++++++++++++++++++++++ 1 file changed, 38 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..2d51867ce90c --- /dev/null +++ b/dynamic_programming/house_robber.py @@ -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() From f5b16b1734abe652fb91085cece7d5797d6f085e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 13 Oct 2025 14:39:09 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dynamic_programming/house_robber.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/dynamic_programming/house_robber.py b/dynamic_programming/house_robber.py index 2d51867ce90c..087adc44d01d 100644 --- a/dynamic_programming/house_robber.py +++ b/dynamic_programming/house_robber.py @@ -20,19 +20,19 @@ """ - -def rob(nums:list[int]) -> int: +def rob(nums: list[int]) -> int: n = len(nums) - if n < 3 : + if n < 3: return max(nums) - dp = [0]*n + 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]) + 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() From 48e6454c8e2db30efbd4a57ad7d1985accfe9e81 Mon Sep 17 00:00:00 2001 From: megha-66 Date: Mon, 13 Oct 2025 14:34:24 +0000 Subject: [PATCH 3/4] New DP Algo added --- dynamic_programming/house_robber.py | 38 +++++++++++++++++++++++++++++ 1 file changed, 38 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..2d51867ce90c --- /dev/null +++ b/dynamic_programming/house_robber.py @@ -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() From 40744acdd37e4abf944e7507f36daecd98fe9010 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 28 Oct 2025 08:53:01 +0000 Subject: [PATCH 4/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dynamic_programming/house_robber.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/dynamic_programming/house_robber.py b/dynamic_programming/house_robber.py index 71dfb5686524..087adc44d01d 100644 --- a/dynamic_programming/house_robber.py +++ b/dynamic_programming/house_robber.py @@ -19,6 +19,7 @@ """ + def rob(nums: list[int]) -> int: n = len(nums) if n < 3: @@ -33,4 +34,5 @@ def rob(nums: list[int]) -> int: if __name__ == "__main__": import doctest + doctest.testmod()