Open fockspaces opened 6 months ago
we can smooth dp without extend its length, if comes to one or two nums length, we just return the maximun elements in list
class Solution:
def rob(self, nums: List[int]) -> int:
if len(nums) <= 2:
return max(nums)
dp = [0] * len(nums)
dp[0], dp[1] = nums[0], max(nums[0], nums[1])
for i in range(2, len(dp)):
dp[i] = max(dp[i - 1], dp[i - 2] + nums[i])
return dp.pop()