#213
House Robber II
medium· 1-D DPruns: 0You are a professional robber planning to rob houses arranged in a circle, where the first and last house are neighbors. Adjacent houses have security systems connected. Given an integer array nums representing the money in each house, return the maximum you can rob tonight without alerting the police.
sign in to paste and practice your own solution
wpm 0acc 100%time 0:000 / 371
class Solution:
def rob(self, nums: list[int]) -> int:
if len(nums) == 1:
return nums[0]
def rob_linear(houses: list[int]) -> int:
prev, curr = 0, 0
for num in houses:
prev, curr = curr, max(curr, prev + num)
return curr
return max(rob_linear(nums[:-1]), rob_linear(nums[1:]))
click the box to focus · tab inserts 4 spaces · backspace to correct · esc to pause
desktop only
codedrill is a typing game and needs a real keyboard. open this on a laptop or desktop to practice.
you can still browse problems and sections from your phone.