Data Structures & Algorithms Dynamic Programming — 1D
💡
Exercise 125

House Robber II 25 XP Medium

LeetCode Ctrl+Enter Run Ctrl+S Save

Same as House Robber, but houses are in a circle. First and last houses are adjacent. Can't rob both.

Solution: run House Robber twice — once on nums[1:] (skip first), once on nums[:-1] (skip last). Take the max.

💡 Pro tip: Understand this problem deeply — don't just memorize the code. Try explaining the approach out loud as if teaching a friend. If you can explain it simply, you truly understand it!

📋 Instructions
Find maximum robbery amount for circular houses. nums = [2,3,2]
Helper function for linear robber. Call it on nums[1:] and nums[:-1]. Return max of both.
🧪 Test Cases
Input
rob([2, 3, 2])
Expected
3
Test case 1
Input
rob([1, 2, 3, 1])
Expected
4
Test case 2
main.py
Hi! I'm Rex 👋
Output
Ready. Press ▶ Run or Ctrl+Enter.