Skip to content
MediumDpAI interview only

House Robber

Asked atamazongooglemicrosoftadobelinkedingoldman-sachs

01 · Problem

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 and it will automatically contact the police if two adjacent houses were broken into on the same night.

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.

02 · Examples

Example 01
Input
nums = [1,2,3,1]
Output
4

Rob houses at index 0 and 2 for a total of 1 + 3 = 4. Robbing houses 1 and 3 gives only 2 + 1 = 3.

Example 02
Input
nums = [2,7,9,3,1]
Output
12

Rob houses at index 0, 2, and 4 for a total of 2 + 9 + 1 = 12. This is better than 7 + 3 = 10.

03 · Constraints

  • 011 <= nums.length <= 100
  • 020 <= nums[i] <= 400

04 · Optimal complexity

Time
O(n)
Space
O(1)
05 · Two ways to work on it

Practice it alone or rehearse it as an interview.

Practice Mode gives you an editor and test runs, nothing else. AI Interview Mode puts a voice interviewer on the other side, adds a clock, and ends with a scored summary of the round.