We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 6571ded commit 8643cc7Copy full SHA for 8643cc7
house-robber-ii/evan.py
@@ -0,0 +1,31 @@
1
+from typing import List
2
+
3
4
+class Solution:
5
+ def rob(self, nums: List[int]) -> int:
6
+ def linear_rob(nums: List[int]) -> int:
7
+ house_length = len(nums)
8
9
+ if house_length == 0:
10
+ return 0
11
+ if house_length == 1:
12
+ return nums[0]
13
+ if house_length == 2:
14
+ return max(nums[0], nums[1])
15
16
+ dp = [nums[0], max(nums[0], nums[1])]
17
18
+ for index in range(2, house_length):
19
+ dp.append(max(dp[index - 1], dp[index - 2] + nums[index]))
20
21
+ return dp[-1]
22
23
24
25
+ if house_length < 3:
26
+ return linear_rob(nums)
27
28
+ first_house_selected_result = linear_rob(nums[:-1])
29
+ second_house_selected_result = linear_rob(nums[1:])
30
31
+ return max(first_house_selected_result, second_house_selected_result)
0 commit comments