Given an integer array nums representing the amount of money of each house, Th program returns the maximum amount of money you can rob tonight without alerting the police. >>> rob([1,2,3,1]) 4 Explanation: Rob house 1 (money = 1) and then rob house
(self, nums: List[int])
| 11 | |
| 12 | """ |
| 13 | def rob(self, nums: List[int]) -> int: |
| 14 | |
| 15 | """ |
| 16 | |
| 17 | Given an integer array nums representing the amount of money of each house, Th program |
| 18 | returns the maximum amount of money you can rob tonight without alerting the police. |
| 19 | |
| 20 | >>> rob([1,2,3,1]) |
| 21 | 4 |
| 22 | Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3). |
| 23 | Total amount you can rob = 1 + 3 = 4. |
| 24 | |
| 25 | >>>rob([2,7,9,3,1]) |
| 26 | 12 |
| 27 | Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1). |
| 28 | Total amount you can rob = 2 + 9 + 1 = 12. |
| 29 | |
| 30 | """ |
| 31 | dp= [0] * (len(nums) + 2) |
| 32 | for i in range(2,len(nums)+2): |
| 33 | dp[i] = max(nums[i-2] + dp[i-2], dp[i-1]) |
| 34 | return dp[len(nums)+1] |
| 35 | |
| 36 | """ |
| 37 |