Problems/Dynamic Programming/Neighborhood Burglary
Dynamic Programming
medium

Neighborhood Burglary

Given a row of houses, each with a certain amount of valuables, determine the maximum amount you can collect without hitting two adjacent houses. This tests dynamic programming skills and optimization techniques.

dynamic programmingarrayoptimizationrecursionmemoization

Problem Statement

You are planning a heist on a street with houses lined up in a row. Each house contains a certain amount of loot. However, there's a catch: the security system is triggered if you rob two adjacent houses. Your goal is to determine the maximum total value of loot you can steal without setting off the alarm.

Example 1
Input: [1, 2, 3, 1]
Output: 4
The optimal strategy is to rob house 1 (value 1) and house 3 (value 3), yielding a total of 4.
Example 2
Input: [2, 7, 9, 3, 1]
Output: 12
The optimal strategy is to rob house 1 (value 2), house 3 (value 9), and house 5 (value 1), yielding a total of 12.
Constraints
  • -1 <= houses.length <= 100
  • -0 <= houses[i] <= 400
  • -The input array contains only non-negative integers.

Brute Force Approach

Imagine trying every single combination of houses to rob. For each combination, check if any two adjacent houses are robbed. If not, calculate the total loot and keep track of the maximum. This is like trying every possible route to a destination, checking if each route is valid, and then finding the route with the shortest distance. This approach is highly inefficient.

TimeO(2^n)
SpaceO(1)

Ready to practice?

Work through this problem with AI coaching and get real-time feedback

Practice This Problem