Show problems as
Classic problem #198MediumArrayDynamic Programming

House Robber

Real-world version: Tower Activation Credits
Tracked in this browser only
Read House Robber on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

Learn the pattern firstBasic Dynamic Programming · Dynamic Programming, animated step by step

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.

Python
def max_credits(credits: list[int]) -> int:
Java
public int maxCredits(int[] credits)