AlgoFrames

House robber

Standard · Write code · about 12 min

nums[i] is the money in house i of a street. Robbing two neighbouring houses on the same night sets off the alarm.

Return the most money you can rob without robbing two neighbours.

Example 1

Input:

[[1,2,3,1]]

Output:

4

Rob houses 0 and 2: 1 + 3 = 4.

Example 2

Input:

[[2,7,9,3,1]]

Output:

12

Rob houses 0, 2 and 4: 2 + 9 + 1 = 12.

Constraints

  • 1 ≤ nums.length ≤ 1,000
  • 0 ≤ nums[i] ≤ 400

Learn the idea first: 1-D DP: house robber, decode ways, jump game II, a visual lesson.

More 1-D DP: house robber, decode ways, jump game II problems