Blind 75
51 / 75Free practice

House Robber II

Medium · Dynamic programming

Jump to code

Houses form a circle and have nonnegative amounts. Maximize the sum of chosen houses without choosing any neighbors, including the first and last together.

Examples

Example 1

nums = [2,3,2]

Output: 3

Example 2

nums = [1,2,3,1]

Output: 4

Constraints

  • 1 <= nums.length <= 100
  • 0 <= nums[i] <= 100
Need a hint?

Every valid answer excludes either the first house or the last.

Real dynamic programming interview questions

Questions on the same topic that candidates reported from real company interviews. Some company questions are locked and open with Premium or XP.

Browse all real coding interview questions

Accepted submissions mark this problem complete. Progress stays in this browser.

Run checks 5 examples. Submit checks all 9 cases. A free account runs this problem; no XP needed.
Language
Case 1
Input
[
  [
    2,
    3,
    2
  ]
]
Expected
3
Case 2
Input
[
  [
    1,
    2,
    3,
    1
  ]
]
Expected
4
Case 3
Input
[
  [
    1,
    2,
    3
  ]
]
Expected
3
Case 4
Input
[
  [
    1
  ]
]
Expected
1
Case 5
Input
[
  [
    2,
    3,
    2,
    3
  ]
]
Expected
6

Drafts are saved per language in this browser when storage is available.