Blind 75
50 / 75Free practice

House Robber

Medium · Dynamic programming

Jump to code

Each house along a street contains a nonnegative amount. Find the maximum total obtainable without choosing two neighboring houses. Choosing none is allowed.

Examples

Example 1

nums = [1,2,3,1]

Output: 4

Example 2

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

Output: 12

Constraints

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

At each house, choose between skipping it and combining it with the best earlier non-neighboring total.

Asked in real interviews

Candidates reported versions of House Robber from these 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 8 cases. A free account runs this problem; no XP needed.
Language
Case 1
Input
[
  [
    1,
    2,
    3,
    1
  ]
]
Expected
4
Case 2
Input
[
  [
    2,
    7,
    9,
    3,
    1
  ]
]
Expected
12
Case 3
Input
[
  [
    1
  ]
]
Expected
1
Case 4
Input
[
  [
    2,
    1,
    1,
    2
  ]
]
Expected
4
Case 5
Input
[
  [
    2,
    4,
    8,
    9,
    9,
    3
  ]
]
Expected
19

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