Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- At each house, you only have two choices:
- Skip it: keep whatever total you had up to the previous house.
- Rob it: take this house’s money + the best total up to two houses back (since you can’t use the previous one).
- You pick whichever is bigger:
- best at house i = max(best at i-1, nums[i] + best at i-2)
- def rob(nums):
- prev2 = 0 # best up to two houses back
- prev1 = 0 # best up to the previous house
- for x in nums:
- current = max(prev1, x + prev2)
- prev2 = prev1
- prev1 = current
- return prev1
Advertisement
Add Comment
Please, Sign In to add comment