Skip to content

Commit a05b534

Browse files
committed
solve house roubber il
1 parent bdab952 commit a05b534

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

house-robber-ii/samthekorean.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)