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 5f84ff8

Browse files
Merge pull request youngyangyang04#2023 from ZerenZhang2022/patch-29
Update 0213.打家劫舍II.md
2 parents 89eb387 + 34aabac commit 5f84ff8

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

‎problems/0213.打家劫舍II.md‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,20 @@ class Solution:
147147
dp[i]=max(dp[i-1],dp[i-2]+nums[i])
148148
return dp[-1]
149149
```
150-
150+
```python
151+
class Solution: # 二维dp数组写法
152+
def rob(self, nums: List[int]) -> int:
153+
if len(nums)<3: return max(nums)
154+
return max(self.default(nums[:-1]),self.default(nums[1:]))
155+
def default(self,nums):
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])
160+
dp[i][1] = dp[i-1][0] + nums[i]
161+
return max(dp[-1])
162+
163+
```
151164
Go:
152165

153166
```go

0 commit comments

Comments
(0)

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