Skip to content

Commit 48e6454

Browse files
committed
New DP Algo added
1 parent e2a78d4 commit 48e6454

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""
2+
You are a professional robber planning to rob houses along a street.
3+
Each house has a certain amount of money stashed,
4+
the only constraint stopping you from robbing each of them is that
5+
adjacent houses have security systems connected
6+
and it will automatically contact the police if two adjacent
7+
houses were broken into on the same night.
8+
9+
Given an integer array nums representing the amount of money of each house, we have to
10+
return the maximum amount of money you can rob tonight without alerting the police.
11+
12+
Source : https://leetcode.com/problems/house-robber?envType=problem-list-v2&envId=dynamic-programming
13+
14+
15+
Example
16+
17+
>>> rob([1,2,3,1])
18+
4
19+
20+
"""
21+
22+
23+
24+
def rob(nums:list[int]) -> int:
25+
n = len(nums)
26+
if n < 3 :
27+
return max(nums)
28+
dp = [0]*n
29+
dp[0] = nums[0]
30+
dp[1] = max(nums[0], nums[1])
31+
for i in range(2,n):
32+
dp[i] = max(nums[i]+dp[i-2], dp[i-1])
33+
return max(dp)
34+
35+
36+
if __name__ == "__main__":
37+
import doctest
38+
doctest.testmod()

0 commit comments

Comments
 (0)