📘
House Robber II
MediumDynamic Programming
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,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(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
Python 3.13.1
Test Results
0/0Run code to see test results.