The Stealthy Thief

MediumAcc. 91.2%
+30 XP 12

The Strategic Selection (House Robber)

You are a strategic collector planning to gather treasures from a row of houses. However, you cannot take from two adjacent houses because it will trigger a global alarm. Your goal is to maximize the value of your haul while respecting the "No Neighbors" rule.

The Assignment

Your mission is to find the maximum possible haul. Your function receives an array of nums (the values in each house).

  1. If the list is empty, return 0. If it has one item, print that value.
  2. Initialize two variables to track the maximum haul possible if you robbed the previous house vs. the one before that.
  3. For each house:
    • Decide between: Robbing current house + Max haul from 2 houses ago VS. Skipping current house and keeping the Max haul from 1 house ago.
  4. Print the final maximum amount.

01EXAMPLE 1

Input[1, 2, 3, 1]
Output4

Explanation: Rob house 1 (1) and house 3 (3). Total 4.

Constraints

  • Solve in O(n) time and O(n) or O(1) space.
AlgorithmsDynamic Programming
JavaScript
Loading...
1 Hidden

Input Arguments

nums[1,2,3,1]

Expected Output

4

Click RUN to test your solution