PIXELBANKv8.2.1
Menu

House Robber II

Same as House Robber, but houses are arranged in a circle (first and last are adjacent). Return the maximum amount you can rob.

Example:

Input:
2,3,2
Output:
3
Reasoning:
  • The input represents the amount of money in each house, arranged in a circle: 2,3,22, 3, 2.
  • To find the maximum amount that can be robbed, we consider two cases: robbing the first house or not robbing the first house. If we rob the first house, we cannot rob the last house.
  • In this case, the maximum amount is obtained by robbing the house with 3,asrobbingthefirstandlasthouses(3, as robbing the first and last houses (2 + 2=2 = 4) is not possible due to their adjacency.
  • The optimal solution is to rob the middle house, resulting in a maximum amount of $3.

Constraints:

  • 1 <= len(nums) <= 100
  • 0 <= nums[i] <= 1000
Editor

Test Results

0/0
Run code to see test results.