house_robber_ii
1from functools import cache 2 3 4# @leet start 5class Solution: 6 def rob(self, nums: list[int]) -> int: 7 """ 8 This question is like house robber, but you're robbing a circular 9 array instead of a line. 10 11 This is the same as house robber 1, but you can pass in a boolean that 12 allows you to see if you've robbed the house before. 13 """ 14 15 @cache 16 def dp(i, robbed): 17 if i >= len(nums) or (i == len(nums) - 1 and robbed): 18 return 0 19 rob_curr = nums[i] + dp(i + 2, robbed if i != 0 else True) 20 rob_next = dp(i + 1, robbed) 21 return max(rob_curr, rob_next) 22 23 return dp(0, False) 24 25 26def test(): 27 assert 2 + 2 == 4
class
Solution:
6class Solution: 7 def rob(self, nums: list[int]) -> int: 8 """ 9 This question is like house robber, but you're robbing a circular 10 array instead of a line. 11 12 This is the same as house robber 1, but you can pass in a boolean that 13 allows you to see if you've robbed the house before. 14 """ 15 16 @cache 17 def dp(i, robbed): 18 if i >= len(nums) or (i == len(nums) - 1 and robbed): 19 return 0 20 rob_curr = nums[i] + dp(i + 2, robbed if i != 0 else True) 21 rob_next = dp(i + 1, robbed) 22 return max(rob_curr, rob_next) 23 24 return dp(0, False)
def
rob(self, nums: list[int]) -> int:
7 def rob(self, nums: list[int]) -> int: 8 """ 9 This question is like house robber, but you're robbing a circular 10 array instead of a line. 11 12 This is the same as house robber 1, but you can pass in a boolean that 13 allows you to see if you've robbed the house before. 14 """ 15 16 @cache 17 def dp(i, robbed): 18 if i >= len(nums) or (i == len(nums) - 1 and robbed): 19 return 0 20 rob_curr = nums[i] + dp(i + 2, robbed if i != 0 else True) 21 rob_next = dp(i + 1, robbed) 22 return max(rob_curr, rob_next) 23 24 return dp(0, False)
This question is like house robber, but you're robbing a circular array instead of a line.
This is the same as house robber 1, but you can pass in a boolean that allows you to see if you've robbed the house before.
def
test():