Merge pull request #2022 from ZerenZhang2022/patch-28

Update 0198.打家劫舍.md
This commit is contained in:
程序员Carl
2023-04-25 09:52:09 +08:00
committed by GitHub

View File

@ -153,7 +153,17 @@ class Solution:
dp[i] = max(dp[i-2]+nums[i], dp[i-1])
return dp[-1]
```
```python
class Solution: # 二维dp数组写法
def rob(self, nums: List[int]) -> int:
dp = [[0,0] for _ in range(len(nums))]
dp[0][1] = nums[0]
for i in range(1,len(nums)):
dp[i][0] = max(dp[i-1][1],dp[i-1][0])
dp[i][1] = dp[i-1][0]+nums[i]
print(dp)
return max(dp[-1])
```
Go
```Go
func rob(nums []int) int {