House Robber
Real-world version: Tower Activation CreditsThe statement here is our own: the same inputs, outputs and technique.
A carrier runs a line of relay towers strung along one highway, listed west to east in the integer array credits. Switching on the tower at position i earns credits[i] units of billable bandwidth for the month.
Towers that sit side by side in the line share an overlapping band. Because of that, whenever you switch a tower on, both towers immediately beside it have to stay dark. Any selection obeying that rule is permitted, and leaving every tower dark is permitted too.
Return the greatest total the carrier can bill.
Examples
Example 1
- Input
- credits = [2, 8, 9, 8, 2]
- Output
- 16
Lighting the second and fourth towers bills 8 + 8 = 16. Grabbing the fat 9 in the centre forces both eights dark and only reaches 9 + 2 + 2 = 13.
Example 2
- Input
- credits = [3, 1, 1, 3, 1, 3]
- Output
- 9
Positions 0, 3 and 5 are pairwise non-neighbouring and bill 3 + 3 + 3 = 9.
Example 3
- Input
- credits = [7]
- Output
- 7
A lone tower has no neighbour to interfere with, so it can be lit.
Constraints
- 1 <= credits.length <= 100
- 0 <= credits[i] <= 400
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def max_credits(credits: list[int]) -> int:public int maxCredits(int[] credits)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 10 test cases: 10 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's