From 801e03ab3112da8e3e1666fafded4aa998836723 Mon Sep 17 00:00:00 2001 From: ZerenZhang2022 <118794589+ZerenZhang2022@users.noreply.github.com> Date: Mon, 10 Apr 2023 00:48:03 -0400 Subject: [PATCH] =?UTF-8?q?Update=200198.=E6=89=93=E5=AE=B6=E5=8A=AB?= =?UTF-8?q?=E8=88=8D.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加python - 二维dp数组写法 --- problems/0198.打家劫舍.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/problems/0198.打家劫舍.md b/problems/0198.打家劫舍.md index fdb2dabf..20e18c08 100644 --- a/problems/0198.打家劫舍.md +++ b/problems/0198.打家劫舍.md @@ -150,7 +150,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 {