AlgoFrames

House robber II: houses in a circle

Challenge · Write code · about 20 min

The houses now stand in a circle: house 0 and the last house are neighbours. nums[i] is the money in house i, and robbing two neighbours sets off the alarm.

Return the most money you can rob.

Example 1

Input:

[[2,3,2]]

Output:

3

Houses 0 and 2 touch, so the best is house 1 alone.

Example 2

Input:

[[1,2,3,1]]

Output:

4

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

Constraints

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

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