Apply the recipe to a new problem: house robber
Challenge · Write code · about 15 min
A street has houses in a row. House i holds loot[i]. Robbing two neighbouring houses sets off an alarm, so you may not rob two houses that are next to each other.
Return the most loot you can take.
This problem is new: write down the state, transition, base cases, order and answer yourself, then code them.
Example 1
Input:
[[2,7,9,3,1]]
Output:
12
Rob houses 0, 2 and 4: 2 + 9 + 1.
Example 2
Input:
[[1,2,3,1]]
Output:
4
Rob houses 0 and 2.
Constraints
- 0 ≤ loot.length ≤ 1,000
- 0 ≤ loot[i] ≤ 1,000
Learn the idea first: The DP recipe: state, transition, base cases, order, answer, a visual lesson.
More The DP recipe: state, transition, base cases, order, answer problems
- Identify the state Warm-up
- Write the transition Standard
- Min cost climbing stairs Standard
- Fill the min-cost table Standard
- Stair points Standard