Most Cash From Non-Neighboring Houses
Problem statement
Houses stand in a row. money[i] is the amount of cash in house i.
You may collect the cash from any set of houses, with one rule: you can never take from two houses that are next to each other. Return the largest total you can collect.
Function
maxLoot(money: int[]) → intExamples
Example 1
money = [3,10,3,1,2]return = 12Take the houses at index 1 (10) and index 4 (2). They are not next to each other, and the total is 12. No other choice does better. For example, indexes 0, 2 and 4 give 3 + 3 + 2 = 8, and indexes 1 and 3 give 10 + 1 = 11.
Example 2
money = [6,1,1,6]return = 12Take the first and the last house: 6 + 6 = 12. Skipping two houses in a row is allowed, and here it pays off. Taking every other house gives only 6 + 1 = 7 or 1 + 6 = 7.
Example 3
money = [5]return = 5There is only one house, so take it.
Constraints
1 <= money.length <= 4000 <= money[i] <= 1000