v9.1.1
Menu

House Robber II

mediumFREEDynamic ProgrammingBlind 75

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

Learn this in

More from Blind 75

solution.py

Test results

Sign in to run your code against the test cases.