FrontendInterviews.dev

Loading problem…

304. House Robber

Medium•

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. The only constraint stopping you from robbing each of them is that adjacent houses have security systems connected — if two adjacent houses were broken into on the same night, the police will be automatically contacted.

Given an integer array nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police.

This is a classic 1D dynamic programming problem. At each house, you decide: rob it (skip the previous) or skip it (keep the previous best).

Requirements

1. Basic Functionality

  • Maximize total money robbed
  • Cannot rob two adjacent houses
  • Handle edge cases (empty, single house)

Example Usage

rob([1, 2, 3, 1]);    // 4 (rob house 0 and 2: 1+3=4)
rob([2, 7, 9, 3, 1]); // 12 (rob house 0, 2, 4: 2+9+1=12)

Real-World Context

  • Resource scheduling: Maximizing revenue with cooldown constraints
  • Job scheduling: Selecting non-conflicting jobs for maximum profit
  • Network throttling: Maximizing throughput with rate limiting

Constraints

  • 0 <= nums.length <= 100
  • 0 <= nums[i] <= 400
Accepted8/11|Acceptance Rate72.7%