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 bdab952 commit a05b534Copy full SHA for a05b534
house-robber-ii/samthekorean.py
@@ -0,0 +1,30 @@
1
+# TC : O(n), where n is the number of houses.
2
+# SC : O(1)
3
+class Solution:
4
+ def rob(self, nums):
5
+ n = len(nums)
6
+ if n == 1:
7
+ return nums[0]
8
+ if n == 2:
9
+ return max(nums[0], nums[1])
10
+ if n == 3:
11
+ return max(nums[0], max(nums[1], nums[2]))
12
+
13
+ a = nums[0]
14
+ b = max(nums[0], nums[1])
15
+ c = -1
16
+ a1 = nums[1]
17
+ b1 = max(nums[1], nums[2])
18
+ c1 = -1
19
20
+ for i in range(2, n):
21
+ if i < n - 1:
22
+ c = max(nums[i] + a, b)
23
+ a = b
24
+ b = c
25
+ if i > 2:
26
+ c1 = max(nums[i] + a1, b1)
27
+ a1 = b1
28
+ b1 = c1
29
30
+ return max(c, c1)
0 commit comments