Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 801e03a

Browse files
Update 0198.打家劫舍.md
增加python - 二维dp数组写法
1 parent ba03a17 commit 801e03a

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

‎problems/0198.打家劫舍.md‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,17 @@ class Solution:
150150
dp[i] = max(dp[i-2]+nums[i], dp[i-1])
151151
return dp[-1]
152152
```
153-
153+
```python
154+
class Solution: # 二维dp数组写法
155+
def rob(self, nums: List[int]) -> int:
156+
dp = [[0,0] for _ in range(len(nums))]
157+
dp[0][1] = nums[0]
158+
for i in range(1,len(nums)):
159+
dp[i][0] = max(dp[i-1][1],dp[i-1][0])
160+
dp[i][1] = dp[i-1][0]+nums[i]
161+
print(dp)
162+
return max(dp[-1])
163+
```
154164
Go:
155165
```Go
156166
func rob(nums []int) int {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /