House Robber LeetCode solution in Python
1class Solution:2def rob(self, nums: List[int]) -> int:3# [rob1,rob2,n,n+1]4# 1 | 2 | 3 | 15# index 0 1 2 36# so upto last index it depends on previous 2 values :7# here upto 2nd index max rob is 1+3=4; not choosing adjacent element 28# and upto 1st index max rob is 2 ; not choosing any adjacent elements9# so at 3rd index it depend on prev rob value and 1st index rob value+last value10# i.e max(2+(val at last index),4)11rob1 = 012rob2 = 013for i in nums:14temp = max(rob1 + i, rob2)15rob1 = rob216rob2 = temp17return rob2