Facebook PixelHouse Robber — Coding Practice
House RobberMedium

House Robber

Medium 21.5k50% acceptance
ArrayDynamic Programming

You are planning to rob houses along a street. Each house holds some amount of money, given by nums. The only constraint is that adjacent houses are wired together — robbing two neighbouring houses on the same night triggers the alarm.

Return the maximum amount of money you can rob without ever robbing two adjacent houses.

Example 1
Input: nums = [1,2,3,1]
Output: 4
Rob house 0 (1) and house 2 (3) for a total of 4; they are not adjacent.
Example 2
Input: nums = [2,7,9,3,1]
Output: 12
Rob houses 0, 2, and 4 → 2 + 9 + 1 = 12.
Example 3
Input: nums = [5]
Output: 5
Constraints
  • 1 ≤ nums.length ≤ 100
  • 0 ≤ nums[i] ≤ 400
Asked atAmazonGoogleMicrosoftLinkedIn
JavaScript
Loading editor…
Case 1
[1,2,3,1]
expected: 4
Case 2
[2,7,9,3,1]
expected: 12
Case 3
[5]
expected: 5
Case 4
[2,1,1,2]
expected: 4
Case 5
[0]
expected: 0
Case 6
[100,1,1,100]
expected: 200
Case 7
[6,7,1,30,8,2,4]
expected: 41