Skip to content

Commit 1cf388e

Browse files
committed
update
1 parent ed74996 commit 1cf388e

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

P105. 打家劫舍 II.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
![algo44](./images/algo44.jpg)
2+
3+
```
4+
class Solution:
5+
def rob(self, nums: List[int]) -> int:
6+
n = len(nums)
7+
if n<=3:
8+
return max(nums)
9+
#偷第一号房
10+
max_1 = [0]*n
11+
max_1[0] = max_1[1] = nums[0]
12+
for i in range(2, n-1):
13+
max_1[i] = max(nums[i]+max_1[i-2], max_1[i-1])
14+
#不偷第一号房
15+
max_0 = [0]*n
16+
max_0[1] = nums[1]
17+
for i in range(2, n):
18+
max_0[i] = max(nums[i]+max_0[i-2], max_0[i-1])
19+
return max(max(max_1), max(max_0))
20+
```

images/algo44.jpg

43.7 KB
Loading

0 commit comments

Comments
 (0)