House Robber II
Explore how to apply dynamic programming to maximize robbery amount when houses are arranged in a circle and adjacent robberies trigger alarms. Learn to handle constraints effectively, develop a clear problem-solving strategy, and implement your solution with hands-on practice.
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.
import java.util.*;class HouseRobber {public static int houseRobber(int[] money) {// Replace this placeholder return statement with your codereturn -1;}}