MEDIUM NC#102 Blind #23 Dynamic Programming 1D
213. House Robber II
๐ Problem
Houses are arranged in a circle (first and last are adjacent). Return the maximum amount of money you can rob without alerting the police.
๐ง Visual Learning Aid
1 Model the input into the right structure
2 Choose the core technique and invariant
3 Execute step-by-step with a sample
4 Validate complexity and edge cases
JS/TS Refreshers
- โขArray methods (`push`, `pop`, `shift`, `slice`)
- โขObject/Map/Set usage patterns
- โขFunction parameter and return typing
- โขArray DP table updates
- โขState transition thinking
- โขBase case initialization
Logical Thinking Concepts
- โขDefine invariants before coding
- โขCheck edge cases first (`[]`, single element, duplicates)
- โขEstimate time/space before implementation
- โขApply Dynamic Programming reasoning pattern
- โขApply Recursion reasoning pattern
๐ก Approach
- โ Two cases: rob houses 0 to n-2, or rob houses 1 to n-1
- โ Use same logic as House Robber I for each case
- โ Return max of both cases
- โ Time: O(n), Space: O(1)
๐งญ Prerequisites
๐ ๏ธ Hints & Pitfalls
Hints
- โขTwo cases: rob houses 0 to n-2, or rob houses 1 to n-1
- โขUse same logic as House Robber I for each case
- โขReturn max of both cases
Common Pitfalls
- โขTime: O(n), Space: O(1)
๐งช Test Cases
Hidden tests on submit: 1
Test Case 1
Not run Input:
rob2([2,3,2]); Expected:
3 Test Case 2
Not run Input:
rob2([1,2,3,1]); Expected:
4 Test Case 3
Not run Input:
rob2([1,2,3]); Expected:
3 ๐ Code Editor
๐ค Output