Maximum Loot on a Line or Circle
Problem statement
Each entry in money is the amount held by one house, in street order. Choose houses with the largest possible total, without choosing two neighboring houses.
When circular is false, the houses form a line. When it is true, the first and last houses are also neighbors if there are at least two houses. A single house does not conflict with itself. You may choose no houses. Return the maximum total; an empty street returns 0.
Function
maxNonAdjacentLoot(money: int[], circular: boolean) → intExamples
Example 1
money = [4,1,2,9]circular = falsereturn = 13Choose the first and fourth houses, worth 4 + 9 = 13. They are not neighbors in a line. No valid choice has a larger total.
Example 2
money = [4,1,2,9]circular = truereturn = 10The first and fourth houses are neighbors in a circle, so they cannot both be chosen. Choosing the second and fourth houses gives 1 + 9 = 10, the maximum.
Example 3
money = [9]circular = truereturn = 9There is only one house. It does not conflict with itself, so choosing it gives 9.
Constraints
0 <= money.length <= 50000 <= money[i] <= 100000circularis a boolean.