Skip to content

Commit

Permalink
solve house roubber il
Browse files Browse the repository at this point in the history
  • Loading branch information
SamTheKorean committed Jul 5, 2024
1 parent bdab952 commit a05b534
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions house-robber-ii/samthekorean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# TC : O(n), where n is the number of houses.
# SC : O(1)
class Solution:
def rob(self, nums):
n = len(nums)
if n == 1:
return nums[0]
if n == 2:
return max(nums[0], nums[1])
if n == 3:
return max(nums[0], max(nums[1], nums[2]))

a = nums[0]
b = max(nums[0], nums[1])
c = -1
a1 = nums[1]
b1 = max(nums[1], nums[2])
c1 = -1

for i in range(2, n):
if i < n - 1:
c = max(nums[i] + a, b)
a = b
b = c
if i > 2:
c1 = max(nums[i] + a1, b1)
a1 = b1
b1 = c1

return max(c, c1)

0 comments on commit a05b534

Please sign in to comment.