FastPrepMaximum Value from Circular Houses
Problem · Dynamic Programming

Maximum Value from Circular Houses

Learn this problem
MediumDigitalOcean logoDigitalOceanFULLTIMEONSITE INTERVIEW

Problem statement

Houses stand in a circle. The nonnegative integer nums[i] is the value available in house i. Select houses with no two adjacent and return the maximum total value.

The first and last houses are adjacent. For this exercise, assume there is at least one house, selecting none is allowed, and a single house may be selected.

Function

robCircular(nums: int[]) → int

Examples

Example 1

nums = [2,3,2]return = 3

The two houses worth 2 are adjacent around the circle, so choose the middle house worth 3.

Example 2

nums = [1,2,3,1]return = 4

Choose values 1 and 3 at indices 0 and 2; they are not adjacent.

Example 3

nums = [5]return = 5

The only house may be selected.

Constraints

  • 1 <= nums.length <= 10^5.
  • 0 <= nums[i] <= 10^4.
  • The result fits in a signed 32-bit integer.

More DigitalOcean problems

drafts saved locally
public int robCircular(int[] nums) {
    // Write your code here.
}
nums[2,3,2]
expected3
checking account