Skip to main content
Back to problems
Leetcode
Medium
Arrays
Dynamic Programming
Google
Meta
House Robber

Choose non-adjacent houses to maximize the total amount of money robbed.

Acceptance 65%
Problem Statement

You are given a row of houses, where each house contains some amount of money. A robber wants to maximize the total amount stolen, but cannot rob two adjacent houses on the same night.

Return the maximum amount of money the robber can steal without triggering the alarm.

Input Format

  • An integer array nums, where nums[i] is the amount of money in the i-th house.
  • Each house is in a straight line, so house i is adjacent to i-1 and i+1.

Output Format

  • Return a single integer: the maximum sum obtainable by choosing houses such that no two chosen houses are adjacent.

Constraints

  • 1 <= nums.length
  • 0 <= nums[i]
  • The answer fits in a 32-bit signed integer.
Examples
Sample cases returned by the problem API.

Example 1

Input

nums = [1,2,3,1]

Output

4

Explanation

Rob houses 1 and 3 (1-indexed), for a total of 1 + 3 = 4.

Example 2

Input

nums = [2,7,9,3,1]

Output

12

Explanation

A best choice is houses with values 2, 9, and 1, giving 12.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.