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 3b3d9c4

Browse files
Update 0213.打家劫舍II.md
1 parent bc13242 commit 3b3d9c4

File tree

1 file changed

+78
-25
lines changed

1 file changed

+78
-25
lines changed

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

Lines changed: 78 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -130,40 +130,93 @@ class Solution {
130130
```
131131

132132
Python:
133+
133134
```Python
134135
class Solution:
135136
def rob(self, nums: List[int]) -> int:
136-
#在198入门级的打家劫舍问题上分两种情况考虑
137-
#一是不偷第一间房,二是不偷最后一间房
138-
if len(nums)==1:#题目中提示nums.length>=1,所以不需要考虑len(nums)==0的情况
137+
iflen(nums) ==0:
138+
return0
139+
if len(nums)==1:
139140
return nums[0]
140-
val1=self.roblist(nums[1:])#不偷第一间房
141-
val2=self.roblist(nums[:-1])#不偷最后一间房
142-
return max(val1,val2)
143-
144-
def roblist(self,nums):
145-
l=len(nums)
146-
dp=[0]*l
147-
dp[0]=nums[0]
148-
for i in range(1,l):
149-
if i==1:
150-
dp[i]=max(dp[i-1],nums[i])
151-
else:
152-
dp[i]=max(dp[i-1],dp[i-2]+nums[i])
153-
return dp[-1]
141+
142+
result1 = self.robRange(nums, 0, len(nums) - 2) # 情况二
143+
result2 = self.robRange(nums, 1, len(nums) - 1) # 情况三
144+
return max(result1, result2)
145+
# 198.打家劫舍的逻辑
146+
def robRange(self, nums: List[int], start: int, end: int) -> int:
147+
if end == start:
148+
return nums[start]
149+
150+
prev_max = nums[start]
151+
curr_max = max(nums[start], nums[start + 1])
152+
153+
for i in range(start + 2, end + 1):
154+
temp = curr_max
155+
curr_max = max(prev_max + nums[i], curr_max)
156+
prev_max = temp
157+
158+
return curr_max
159+
154160
```
161+
2维DP
155162
```python
156-
class Solution:# 二维dp数组写法
163+
class Solution:
157164
def rob(self, nums: List[int]) -> int:
158-
if len(nums)<3: return max(nums)
159-
return max(self.default(nums[:-1]),self.default(nums[1:]))
160-
def default(self,nums):
161-
dp = [[0,0] for _ in range(len(nums))]
165+
if len(nums) < 3:
166+
return max(nums)
167+
168+
# 情况二:不抢劫第一个房屋
169+
result1 = self.robRange(nums[:-1])
170+
171+
# 情况三:不抢劫最后一个房屋
172+
result2 = self.robRange(nums[1:])
173+
174+
return max(result1, result2)
175+
176+
def robRange(self, nums):
177+
dp = [[0, 0] for _ in range(len(nums))]
162178
dp[0][1] = nums[0]
163-
for i in range(1,len(nums)):
164-
dp[i][0] = max(dp[i-1])
165-
dp[i][1] = dp[i-1][0] + nums[i]
179+
180+
for i in range(1, len(nums)):
181+
dp[i][0] = max(dp[i - 1])
182+
dp[i][1] = dp[i - 1][0] + nums[i]
183+
166184
return max(dp[-1])
185+
186+
187+
188+
```
189+
190+
优化版
191+
```python
192+
class Solution:
193+
def rob(self, nums: List[int]) -> int:
194+
if not nums: # 如果没有房屋,返回0
195+
return 0
196+
197+
if len(nums) == 1: # 如果只有一个房屋,返回该房屋的金额
198+
return nums[0]
199+
200+
# 情况二:不抢劫第一个房屋
201+
prev_max = 0 # 上一个房屋的最大金额
202+
curr_max = 0 # 当前房屋的最大金额
203+
for num in nums[1:]:
204+
temp = curr_max # 临时变量保存当前房屋的最大金额
205+
curr_max = max(prev_max + num, curr_max) # 更新当前房屋的最大金额
206+
prev_max = temp # 更新上一个房屋的最大金额
207+
result1 = curr_max
208+
209+
# 情况三:不抢劫最后一个房屋
210+
prev_max = 0 # 上一个房屋的最大金额
211+
curr_max = 0 # 当前房屋的最大金额
212+
for num in nums[:-1]:
213+
temp = curr_max # 临时变量保存当前房屋的最大金额
214+
curr_max = max(prev_max + num, curr_max) # 更新当前房屋的最大金额
215+
prev_max = temp # 更新上一个房屋的最大金额
216+
result2 = curr_max
217+
218+
return max(result1, result2)
219+
167220

168221
```
169222
Go:

0 commit comments

Comments
(0)

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