Skip to content
BASA DSA

House Robber II

Medium1-D Dynamic Programming

Choose houses on a circle without choosing adjacent houses, including the first and last pair. Return the greatest sum of their nonnegative values.

Example 1

Input
nums = [4,1,1,4]
Output
5

Explanation

The first and last 4 are adjacent on the circle; combine either with the opposite interior 1 for 5.

Example 2

Input
nums = [8]
Output
8

Explanation

A singleton allows taking its only house for 8.

Example 3

Input
nums = [2,3,2]
Output
3

Explanation

The endpoint 2s are adjacent, so taking the middle 3 is better.

Constraints

  • 1 <= len(nums) <= 100
  • Values are 0..1000

Function contractrobArguments: json
Loading draft…
SolutionRuns on your device
Loading draft…