Skip to content

Commit

Permalink
Create house-robber-II.py
Browse files Browse the repository at this point in the history
Same as previous house robber solution but houses are arranged circularly
  • Loading branch information
gabedonnan authored Jan 12, 2023
1 parent 0e00f47 commit ede276e
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions house-robber-II.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution(object):
def rob(self, nums):
return max(nums[0] + self.circle(nums[2:-1]), self.circle(nums[1:]))

def circle(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
l = 0
r = 0
if len(nums) == 1:
return nums[0]
for num in nums:
l, r = r, max(l + num, r)
return r

0 comments on commit ede276e

Please sign in to comment.