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

Maximize the amount robbed from houses arranged in a circle without robbing adjacent houses.

Acceptance 0%
Problem Statement

House Robber II

You are given an array nums where nums[i] is the amount of money in the ii-th house. The houses are arranged in a circle, so the first and last houses are also adjacent.

A robber wants to steal from some houses without ever robbing two adjacent houses on the same night.

Return the maximum amount of money the robber can steal.

Because the houses form a circle, you cannot rob both the first and the last house.

Input Format

  • An integer array nums.
  • Each value represents the money in one house.
  • Houses are arranged in a circle.

Output Format

  • Return a single integer: the maximum amount of money that can be robbed without taking from adjacent houses.

Constraints

  • 1nums.length1 \le nums.length
  • The houses are in a circular arrangement.
  • Adjacent houses cannot both be robbed.
  • Use 32-bit integer-safe arithmetic unless otherwise stated.
Examples
Sample cases returned by the problem API.

Example 1

Input

nums = [2,3,2]

Output

3

Explanation

You cannot rob both 2-valued houses because the first and last are adjacent. The best choice is to rob only the house with 3.

Example 2

Input

nums = [1,2,3,1]

Output

4

Explanation

Rob houses with values 1 and 3, or 2 and 1. The best total is 4.

Show 1 more example

Example 3

Input

nums = [1,2,3]

Output

3

Explanation

Because the houses are circular, the first and last are adjacent. The best valid choice is to rob the house with value 3.

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.