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.
We'll cover the following...
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:
-
money.length -
money[i]
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
What is the output if the following list is given as input?
money = [2, 5, 3, 6]
11
9
10
14
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.
Try it yourself
Implement your solution in the following coding playground.
function houseRobber(money) {// Replace this placeholder return statement with your codereturn -1;}export { houseRobber };