Java Dynamic Programming Program · OT54
House Robber Problem in Java
At each house you either skip it and keep the best total so far, or rob it and add its money to the best total from two houses back.
Problem Statement
Each house in a row holds some money. Robbing two neighbouring houses triggers an alarm. Return the largest total you can take without robbing any two adjacent houses.
This is the same as the maximum sum of non-adjacent elements. An empty row gives 0.
Input/Output Examples
[6, 1, 2, 7]Output13Explanation: rob houses 1 and 4: 6 + 7.[5]Output5Explanation: rob it.[1, 3, 1, 3, 100]Output103Explanation: 3 + 100 beats 1 + 1 + 100.Visual Explanation
Only the best totals for the previous two houses are needed at each step, so two variables replace the whole table.
Approaches
Both approaches use the same choice: rob or skip. One works bottom-up with two variables; the other recurses with a cache.
Approach 1: Two Rolling Variables (Recommended)
Idea: current = max(prev1, prev2 + money), then shift the two variables forward.
- Set
prev2 = 0andprev1 = 0. - For each house, compute
max(prev1, prev2 + money). - Shift:
prev2 = prev1,prev1 = current.
Time: O(n) · Space: O(1).
Approach 2: Memoised Recursion
Idea: best(i) = max(best(i + 1), money[i] + best(i + 2)), cached for each index.
- Return 0 past the last house.
- Return the cached value if there is one.
- Otherwise compute and store the better of skipping and robbing.
Time: O(n) · Space: O(n).
Java Solutions
Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.
Approach 1: Two Rolling Variables (Recommended)
The smallest possible state for this recurrence.
import java.util.Arrays;
public class RobberTwoVars {
// best(i) = max(skip house i, rob house i + best(i - 2)); only two previous values are needed
static int rob(int[] houses) {
int prev2 = 0; // best up to two houses back
int prev1 = 0; // best up to the previous house
for (int money : houses) {
int current = Math.max(prev1, prev2 + money);
prev2 = prev1;
prev1 = current;
}
return prev1;
}
public static void main(String[] args) {
int[][] inputs = {{6, 1, 2, 7}, {5}, {1, 3, 1, 3, 100}};
for (int[] h : inputs) {
System.out.println(Arrays.toString(h) + " -> " + rob(h));
}
}
}
[6, 1, 2, 7] -> 13 [5] -> 5 [1, 3, 1, 3, 100] -> 103
Time complexity: O(n) · Space complexity: O(1).
Approach 2: Memoised Recursion
Reads like the problem statement, which makes the recurrence easy to derive.
import java.util.Arrays;
public class RobberMemo {
// Top-down: from house i, either skip it or rob it and jump to i + 2
static int rob(int[] houses) {
int[] memo = new int[houses.length];
Arrays.fill(memo, -1);
return best(houses, 0, memo);
}
private static int best(int[] h, int i, int[] memo) {
if (i >= h.length) {
return 0;
}
if (memo[i] >= 0) {
return memo[i];
}
return memo[i] = Math.max(best(h, i + 1, memo), h[i] + best(h, i + 2, memo));
}
public static void main(String[] args) {
int[][] inputs = {{6, 1, 2, 7}, {5}, {1, 3, 1, 3, 100}};
for (int[] h : inputs) {
System.out.println(Arrays.toString(h) + " -> " + rob(h));
}
}
}
[6, 1, 2, 7] -> 13 [5] -> 5 [1, 3, 1, 3, 100] -> 103
Time complexity: O(n) · Space complexity: O(n).
Complexity Analysis
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Two Rolling Variables | O(n) | O(1) |
| Memoised Recursion | O(n) | O(n) |
Here n is the number of houses. Each house is considered once; the recursive version also keeps a cache and a call stack.
Interview Tips and Recommended Approach
- State the choice clearly: skip this house, or take it and skip the previous one.
- Do not use a greedy rule. Taking every other house fails on
[1, 3, 1, 3, 100]. - Know the circular version. If the first and last houses are neighbours, solve twice: without the first, and without the last.
- Know the tree version, where each node returns two values: robbed and not robbed.
Recommended Approach: Two Rolling Variables
Why: it solves the problem in one pass with two variables and is a clear first example of dynamic programming.