Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

House Robber

Medium Acceptance 51.45% Points 30.00

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.

Hints
Hint 1
  • Hint 1: At each house, you have a choice between taking its money or skipping it.
Hint 2
  • Hint 2: Think about how the best result up to the current house depends on earlier houses.
Hint 3
  • Hint 3: Try keeping only the small amount of information needed to make the next decision.
Constraints
    • 1 = nums.length = 105
    • 0 = nums[i] = 104
Companies
Apple Morgan Stanly
Topics
Dynamic Programming
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions