Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

House Robber

Medium Acceptance 51.45% Points 30

You are given an integer array nums representing the amount of money in each house along a street. You cannot rob two adjacent houses.

Your task is to return the maximum amount of money you can rob without alerting the police.

Examples
Example 1

image Input: nums = [1,2,3,1]
Output: 4
Explanation: Rob house 0 (1) and house 2 (3) for a total of 4.

Example 2

image Input: nums = [2,7,9,3,1]
Output: 12
Explanation: Rob houses 0, 2, and 4 for a total of 2 + 9 + 1 = 12.

Example 3

image Input: nums = [5]
Output: 5
Explanation: With only one house, rob it for a total of 5.

Constraints
    • 1 = nums.length = 105
    • 0 = nums[i] = 104
Hints
    • Hint 1: At each house, you have a choice between taking its money or skipping it.
    • Hint 2: Think about how the best result up to the current house depends on earlier houses.
    • Hint 3: Try keeping only the small amount of information needed to make the next decision.
Topics
Dynamic Programming
Companies
Apple Morgan Stanly
Added 16 Aug 2026 51.45% acceptance
Ln 1, Col 1Sign in to save your code
1 2 3 1
4