Search⌘ K
AI Features

House Robber II

Explore how to apply dynamic programming to solve the House Robber II problem, where houses are arranged in a circle and adjacent robberies are forbidden. Understand constraints, build a clear problem-solving approach, and implement an efficient algorithm to find the maximum amount that can be stolen without triggering alarms.

Statement

A professional robber plans to rob some houses along a street. These houses are arranged in a circle, which means that the first and the last house are neighbors. The robber cannot rob adjacent houses because they have security alarms installed.

Following the constraints mentioned above and given an integer array money representing the amount of money in each house, return the maximum amount the robber can steal without alerting the police.

Constraints:

  • 11\leq money.length 103\leq 10^3
  • 00\leq money[i] 103\leq 10^3

Examples

Understand the problem

Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:

House Robber II

1.

What is the output if the following list is given as input?

money = [2, 5, 3, 6]

A.

11

B.

9

C.

10

D.

14


1 / 2

Figure it out!

We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.

Sequence - Vertical
Drag and drop the cards to rearrange them in the correct sequence.

1
2
3
4
5
6

Try it yourself

Implement your solution in the following coding playground.

JavaScript
usercode > main.js
function houseRobber(money) {
// Replace this placeholder return statement with your code
return -1;
}
export { houseRobber };
House Robber II