House Robber - Maximum possible stolen value
Last Updated :
01 Oct, 2024
There are n houses built in a line, each of which contains some money in it. A robber wants to steal money from these houses, but he can’t steal from two adjacent houses. The task is to find the maximum amount of money which can be stolen.
Examples:
Input: hval[] = {6, 7, 1, 3, 8, 2, 4}
Output: 19
Explanation: The thief will steal from house 1, 3, 5 and 7, total money = 6 + 1 + 8 + 4 = 19.
Input: hval[] = {5, 3, 4, 11, 2}
Output: 16
Explanation: Thief will steal from house 1 and 4, total money = 5 + 11 = 16.
Given an array, the solution is to find the maximum sum subsequence where no two selected elements are adjacent.
[Naive Approach] Using Recursion- O(2n) Time and O(n) Space
The idea is to explore all the possibilities for each house using Recursion. We can start from the last house and for each house, we have two choices:
- Rob the current house and skip the house just before it.
- Skip the current house and move to the next house.
So, the recurrence relation will be:
maxLootRec(n) = max(hval[n - 1] + maxLootRec(n - 2), maxLootRec(n - 1)),
where maxLootRec(n) returns the maximum amount of money which can be stolen if n houses are left.
C++
// C++ Program to solve House Robber Problem using Recursion
#include <iostream>
#include <vector>
using namespace std;
// Calculate the maximum stolen value recursively
int maxLootRec(vector<int> &hval, int n) {
// If no houses are left, return 0.
if (n <= 0) return 0;
// If only 1 house is left, rob it.
if (n == 1) return hval[0];
// Two Choices: Rob the nth house and do not rob the nth house
int pick = hval[n - 1] + maxLootRec(hval, n - 2);
int notPick = maxLootRec(hval, n - 1);
// Return the max of two choices
return max(pick, notPick);
}
// Function to calculate the maximum stolen value
int maxLoot(vector<int>& hval) {
int n = hval.size();
// Call the recursive function for n houses
return maxLootRec(hval, n);
}
int main() {
vector<int> hval = {6, 7, 1, 3, 8, 2, 4};
cout << maxLoot(hval);
return 0;
}
C
// C Program to solve House Robber Problem using Recursion
#include <stdio.h>
// Function to calculate the maximum stolen value
int maxLoot(int *hval, int n) {
// If no houses are left, return 0.
if (n <= 0) return 0;
// If only 1 house is left, rob it.
if (n == 1) return hval[0];
// Two Choices: Rob the nth house and do not rob the nth house
int pick = hval[n - 1] + maxLoot(hval, n - 2);
int notPick = maxLoot(hval, n - 1);
// Return the max of two choices
return (pick > notPick) ? pick : notPick;
}
int main() {
int hval[] = {6, 7, 1, 3, 8, 2, 4};
int n = sizeof(hval) / sizeof(hval[0]);
printf("%d\n", maxLoot(hval, n));
return 0;
}
Java
// Java Program to solve House Robber Problem using Recursion
class GfG {
// Calculate the maximum stolen value recursively
static int maxLootRec(int[] hval, int n) {
// If no houses are left, return 0.
if (n <= 0) return 0;
// If only 1 house is left, rob it.
if (n == 1) return hval[0];
// Two Choices: Rob the nth house and do not rob the nth house
int pick = hval[n - 1] + maxLootRec(hval, n - 2);
int notPick = maxLootRec(hval, n - 1);
// Return the max of two choices
return Math.max(pick, notPick);
}
// Function to calculate the maximum stolen value
static int maxLoot(int[] hval) {
int n = hval.length;
// Call the recursive function for n houses
return maxLootRec(hval, n);
}
public static void main(String[] args) {
int[] hval = {6, 7, 1, 3, 8, 2, 4};
System.out.println(maxLoot(hval));
}
}
Python
# Python Program to solve House Robber Problem using Recursion
# Calculate the maximum stolen value recursively
def maxLootRec(hval, n):
# If no houses are left, return 0.
if n <= 0:
return 0
# If only 1 house is left, rob it.
if n == 1:
return hval[0]
# Two Choices: Rob the nth house and do not rob the nth house
pick = hval[n - 1] + maxLootRec(hval, n - 2)
notPick = maxLootRec(hval, n - 1)
# Return the max of two choices
return max(pick, notPick)
# Function to calculate the maximum stolen value
def maxLoot(hval):
n = len(hval)
# Call the recursive function for n houses
return maxLootRec(hval, n)
if __name__ == "__main__":
hval = [6, 7, 1, 3, 8, 2, 4]
print(maxLoot(hval))
C#
// C# Program to solve House Robber Problem using Recursion
using System;
class GfG {
// Calculate the maximum stolen value recursively
static int MaxLootRec(int[] hval, int n) {
// If no houses are left, return 0.
if (n <= 0) return 0;
// If only 1 house is left, rob it.
if (n == 1) return hval[0];
// Two Choices: Rob the nth house and do not rob the nth house
int pick = hval[n - 1] + MaxLootRec(hval, n - 2);
int notPick = MaxLootRec(hval, n - 1);
// Return the max of two choices
return Math.Max(pick, notPick);
}
// Function to calculate the maximum stolen value
static int MaxLoot(int[] hval) {
int n = hval.Length;
// Call the recursive function for n houses
return MaxLootRec(hval, n);
}
static void Main() {
int[] hval = { 6, 7, 1, 3, 8, 2, 4 };
Console.WriteLine(MaxLoot(hval));
}
}
JavaScript
// JavaScript Program to solve House Robber Problem using Recursion
// Calculate the maximum stolen value recursively
function maxLootRec(hval, n) {
// If no houses are left, return 0.
if (n <= 0) return 0;
// If only 1 house is left, rob it.
if (n === 1) return hval[0];
// Two Choices: Rob the nth house and do not rob the nth house
let pick = hval[n - 1] + maxLootRec(hval, n - 2);
let notPick = maxLootRec(hval, n - 1);
// Return the max of two choices
return Math.max(pick, notPick);
}
// Function to calculate the maximum stolen value
function maxLoot(hval) {
let n = hval.length;
// Call the recursive function for n houses
return maxLootRec(hval, n);
}
let hval = [6, 7, 1, 3, 8, 2, 4];
console.log(maxLoot(hval));
Time Complexity: O(2n). Every house has 2 choices to pick and not pick.
Auxiliary Space: O(n). For recursion stack space
[Better Approach] Using Memoization - O(n) Time and O(n) Space
The above solution has optimal substructure and overlapping subproblems. See the below recursion tree, maxLootRec(2) is being evaluated twice.
Recursion Tree for House RobberWe can optimize this solution using a memo array of size (n + 1), such that memo[i] represents the maximum value that can be collected from first i houses. Please note that there is only one parameter that changes in recursion and the range of this parameter is from 0 to n.
C++
// C++ Program to solve House Robber Problem using Memoization
#include <iostream>
#include <vector>
using namespace std;
int maxLootRec(const vector<int>& hval, int n, vector<int>& memo) {
if (n <= 0) return 0;
if (n == 1) return hval[0];
// Check if the result is already computed
if (memo[n] != -1) return memo[n];
int pick = hval[n - 1] + maxLootRec(hval, n - 2, memo);
int notPick = maxLootRec(hval, n - 1, memo);
// Store the max of two choices in the memo array and return it
memo[n] = max(pick, notPick);
return memo[n];
}
int maxLoot(vector<int>& hval) {
int n = hval.size();
// Initialize memo array with -1
vector<int> memo(n + 1, -1);
return maxLootRec(hval, n, memo);
}
int main() {
vector<int> hval = {6, 7, 1, 3, 8, 2, 4};
cout << maxLoot(hval);
return 0;
}
C
// C Program to solve House Robber Problem using Memoization
#include <stdio.h>
#include <stdlib.h>
int maxLootRec(const int* hval, int n, int* memo) {
if (n <= 0) return 0;
if (n == 1) return hval[0];
// Check if the result is already computed
if (memo[n] != -1) return memo[n];
int pick = hval[n - 1] + maxLootRec(hval, n - 2, memo);
int notPick = maxLootRec(hval, n - 1, memo);
// Store the max of two choices in the memo array and return it
memo[n] = (pick > notPick) ? pick : notPick;
return memo[n];
}
int maxLoot(int* hval, int n) {
// Initialize memo array with -1
int memo[n + 1];
for (int i = 0; i <= n; ++i) {
memo[i] = -1;
}
int result = maxLootRec(hval, n, memo);
return result;
}
int main() {
int hval[] = {6, 7, 1, 3, 8, 2, 4};
int n = sizeof(hval) / sizeof(hval[0]);
printf("%d\n", maxLoot(hval, n));
return 0;
}
Java
// Java Program to solve House Robber Problem using Memoization
import java.util.Arrays;
class GfG {
static int maxLootRec(int[] hval, int n, int[] memo) {
if (n <= 0) return 0;
if (n == 1) return hval[0];
// Check if the result is already computed
if (memo[n] != -1) return memo[n];
int pick = hval[n - 1] + maxLootRec(hval, n - 2, memo);
int notPick = maxLootRec(hval, n - 1, memo);
// Store the max of two choices in the memo array and return it
memo[n] = Math.max(pick, notPick);
return memo[n];
}
// Function to calculate the maximum stolen value
static int maxLoot(int[] hval) {
int n = hval.length;
// Initialize memo array with -1
int[] memo = new int[n + 1];
Arrays.fill(memo, -1);
return maxLootRec(hval, n, memo);
}
public static void main(String[] args) {
int[] hval = {6, 7, 1, 3, 8, 2, 4};
System.out.println(maxLoot(hval));
}
}
Python
# Python Program to solve House Robber Problem using Memoization
def maxLootRec(hval, n, memo):
if n <= 0:
return 0
if n == 1:
return hval[0]
# Check if the result is already computed
if memo[n] != -1:
return memo[n]
pick = hval[n - 1] + maxLootRec(hval, n - 2, memo)
notPick = maxLootRec(hval, n - 1, memo)
# Store the max of two choices in the memo array and return it
memo[n] = max(pick, notPick)
return memo[n]
def maxLoot(hval):
n = len(hval)
# Initialize memo array with -1
memo = [-1] * (n + 1)
return maxLootRec(hval, n, memo)
if __name__ == "__main__":
hval = [6, 7, 1, 3, 8, 2, 4]
print(maxLoot(hval))
C#
// C# Program to solve House Robber Problem using Memoization
using System;
class GfG {
static int MaxLootRec(int[] hval, int n, int[] memo) {
if (n <= 0) return 0;
if (n == 1) return hval[0];
// Check if the result is already computed
if (memo[n] != -1) return memo[n];
int pick = hval[n - 1] + MaxLootRec(hval, n - 2, memo);
int notPick = MaxLootRec(hval, n - 1, memo);
// Store the max of two choices in the memo array and return it
memo[n] = Math.Max(pick, notPick);
return memo[n];
}
// Function to calculate the maximum stolen value
static int MaxLoot(int[] hval) {
int n = hval.Length;
// Initialize memo array with -1
int[] memo = new int[n + 1];
for (int i = 0; i <= n; i++) {
memo[i] = -1;
}
return MaxLootRec(hval, n, memo);
}
static void Main() {
int[] hval = { 6, 7, 1, 3, 8, 2, 4 };
Console.WriteLine(MaxLoot(hval));
}
}
JavaScript
// JavaScript Program to solve House Robber Problem using Memoization
function maxLootRec(hval, n, memo) {
if (n <= 0) return 0;
if (n === 1) return hval[0];
// Check if the result is already computed
if (memo[n] !== -1) return memo[n];
const pick = hval[n - 1] + maxLootRec(hval, n - 2, memo);
const notPick = maxLootRec(hval, n - 1, memo);
// Store the max of two choices in the memo array and return it
memo[n] = Math.max(pick, notPick);
return memo[n];
}
// Function to calculate the maximum stolen value
function maxLoot(hval) {
const n = hval.length;
// Initialize memo array with -1
const memo = new Array(n + 1).fill(-1);
return maxLootRec(hval, n, memo);
}
const hval = [6, 7, 1, 3, 8, 2, 4];
console.log(maxLoot(hval));
Time Complexity: O(n). Every house is computed only once.
Auxiliary Space: O(n). For recursion stack space and memo array.
[Expected Approach 1] Using Tabulation - O(n) Time and O(n) Space
The idea is to build the solution in bottom-up manner. We create a dp[] array of size n+1 where dp[i] represents the maximum value that can be collected with first i houses. We first fill the known values, dp[0] and dp[1] and then fill the remaining values using the formula: dp[i] = max(hval[i] + dp[i - 2], dp[i - 1]). The final result will be stored at dp[n].
C++
// C++ Program to solve House Robber Problem using Tabulation
#include <iostream>
#include <vector>
using namespace std;
// Function to calculate the maximum stolen value using bottom-up DP
int maxLoot(vector<int>& hval) {
int n = hval.size();
// Create a dp array to store the maximum loot at each house
vector<int> dp(n+1, 0);
// Base cases
dp[0] = 0;
dp[1] = hval[0];
// Fill the dp array using the bottom-up approach
for (int i = 2; i <= n; i++)
dp[i] = max(hval[i - 1] + dp[i - 2], dp[i - 1]);
return dp[n];
}
int main() {
vector<int> hval = {6, 7, 1, 3, 8, 2, 4};
cout << maxLoot(hval) << endl;
return 0;
}
C
// C Program to solve House Robber Problem using Tabulation
#include <stdio.h>
int max(int a, int b) {return (a > b) ? a : b;}
int maxLoot(int* hval, int n) {
// Create a dp array to store the
// maximum loot at each house
int dp[n+1];
dp[0] = 0;
dp[1] = hval[0];
// Fill the dp array using the
// bottom-up approach
for (int i = 2; i <= n; i++)
dp[i] = max(hval[i - 1] + dp[i - 2], dp[i - 1]);
return dp[n];
}
int main() {
int hval[] = {6, 7, 1, 3, 8, 2, 4};
int n = sizeof(hval) / sizeof(hval[0]);
printf("%d\n", maxLoot(hval, n));
return 0;
}
Java
// Java Program to solve House Robber Problem using Tabulation
class GfG {
// Function to calculate the maximum stolen value using bottom-up DP
static int maxLoot(int[] hval) {
int n = hval.length;
// Create a dp array to store the maximum loot at each house
int[] dp = new int[n + 1];
// Base cases
dp[0] = 0;
dp[1] = hval[0];
// Fill the dp array using the bottom-up approach
for (int i = 2; i <= n; i++) {
dp[i] = Math.max(hval[i - 1] + dp[i - 2], dp[i - 1]);
}
return dp[n];
}
public static void main(String[] args) {
int[] hval = {6, 7, 1, 3, 8, 2, 4};
System.out.println(maxLoot(hval));
}
}
Python
# Python Program to solve House Robber Problem using Tabulation
def maxLoot(hval):
n = len(hval)
# Create a dp array to store the maximum loot at each house
dp = [0] * (n + 1)
# Base cases
dp[0] = 0
dp[1] = hval[0]
# Fill the dp array using the bottom-up approach
for i in range(2, n + 1):
dp[i] = max(hval[i - 1] + dp[i - 2], dp[i - 1])
return dp[n]
hval = [6, 7, 1, 3, 8, 2, 4]
print(maxLoot(hval))
C#
// C# Program to solve House Robber Problem using Tabulation
using System;
class GfG {
// Function to calculate the maximum stolen value using bottom-up DP
static int MaxLoot(int[] hval) {
int n = hval.Length;
// Create a dp array to store the maximum loot at each house
int[] dp = new int[n + 1];
// Base cases
dp[0] = 0;
dp[1] = hval[0];
// Fill the dp array using the bottom-up approach
for (int i = 2; i <= n; i++) {
dp[i] = Math.Max(hval[i - 1] + dp[i - 2], dp[i - 1]);
}
return dp[n];
}
static void Main() {
int[] hval = { 6, 7, 1, 3, 8, 2, 4 };
Console.WriteLine(MaxLoot(hval));
}
}
JavaScript
// JavaScript Program to solve House Robber Problem using Tabulation
function maxLoot(hval) {
const n = hval.length;
// Create a dp array to store the maximum loot at each house
const dp = new Array(n + 1).fill(0);
// Base cases
dp[0] = 0;
dp[1] = hval[0];
// Fill the dp array using the bottom-up approach
for (let i = 2; i <= n; i++)
dp[i] = Math.max(hval[i - 1] + dp[i - 2], dp[i - 1]);
return dp[n];
}
const hval = [6, 7, 1, 3, 8, 2, 4];
console.log(maxLoot(hval));
Time Complexity: O(n), Every house is computed only once.
Auxiliary Space O(n), We are using a dp array of size n.
[Expected Approach 2] Space-Optimized DP - O(n) Time and O(1) Space
On observing the dp[] array in the previous approach, it can be seen that the answer at the current index depends only on the last two values. In other words, dp[i] depends only on dp[i - 1] and dp[i - 2]. So, instead of storing the result in an array, we can simply use two variables to store the last and second last result.
C++
// C++ Program to solve House Robber Problem using
// Space Optimized Tabulation
#include <iostream>
#include <vector>
using namespace std;
// Function to calculate the maximum stolen value
int maxLoot(vector<int> &hval) {
int n = hval.size();
if (n == 0)
return 0;
if (n == 1)
return hval[0];
// Set previous 2 values
int secondLast = 0, last = hval[0];
// Compute current value using previous two values
// The final current value would be our result
int res;
for (int i = 1; i < n; i++) {
res = max(hval[i] + secondLast, last);
secondLast = last;
last = res;
}
return res;
}
int main() {
vector<int> hval = {6, 7, 1, 3, 8, 2, 4};
cout << maxLoot(hval) << endl;
return 0;
}
C
// C Program to solve House Robber Problem using
// Space Optimized Tabulation
#include <stdio.h>
int max(int a, int b) { return (a > b) ? a : b; }
// Function to calculate the maximum stolen value
int maxLoot(int hval[], int n) {
if (n == 0)
return 0;
if (n == 1)
return hval[0];
// Set previous 2 values
int secondLast = 0, last = hval[0];
// Compute current value using previous
// two values. The final current value
// would be our result
int res;
for (int i = 1; i < n; i++) {
res = max(hval[i] + secondLast, last);
secondLast = last;
last = res;
}
return res;
}
int main() {
int hval[] = {6, 7, 1, 3, 8, 2, 4};
int n = sizeof(hval) / sizeof(hval[0]);
printf("%d\n", maxLoot(hval, n));
return 0;
}
Java
// Java Program to solve House Robber Problem using
// Space Optimized Tabulation
import java.util.Arrays;
class GfG {
// Function to calculate the maximum stolen value
static int maxLoot(int[] hval) {
int n = hval.length;
if (n == 0)
return 0;
if (n == 1)
return hval[0];
// Set previous 2 values
int secondLast = 0, last = hval[0];
// Compute current value using previous
// two values. The final current value
// would be our result
int res = 0;
for (int i = 1; i < n; i++) {
res = Math.max(hval[i] + secondLast, last);
secondLast = last;
last = res;
}
return res;
}
public static void main(String[] args) {
int[] hval = {6, 7, 1, 3, 8, 2, 4};
System.out.println(maxLoot(hval));
}
}
Python
# Python Program to solve House Robber Problem using
# Space Optimized Tabulation
# Function to calculate the maximum stolen value
def maxLoot(hval):
n = len(hval)
if n == 0:
return 0
if n == 1:
return hval[0]
# Set previous 2 values
secondLast = 0
last = hval[0]
# Compute current value using previous two values
# The final current value would be our result
res = 0
for i in range(1, n):
res = max(hval[i] + secondLast, last)
secondLast = last
last = res
return res
hval = [6, 7, 1, 3, 8, 2, 4]
print(maxLoot(hval))
C#
// C# Program to solve House Robber Problem using
// Space Optimized Tabulation
using System;
class GfG {
// Function to calculate the maximum stolen value
static int maxLoot(int[] hval) {
int n = hval.Length;
if (n == 0)
return 0;
if (n == 1)
return hval[0];
// Set previous 2 values
int secondLast = 0, last = hval[0];
// Compute current value using previous two values
// The final current value would be our result
int res = 0;
for (int i = 1; i < n; i++) {
res = Math.Max(hval[i] + secondLast, last);
secondLast = last;
last = res;
}
return res;
}
static void Main() {
int[] hval = { 6, 7, 1, 3, 8, 2, 4 };
Console.WriteLine(maxLoot(hval));
}
}
JavaScript
// Function to calculate the maximum stolen value
function maxLoot(hval) {
const n = hval.length;
if (n === 0)
return 0;
if (n === 1)
return hval[0];
// Set previous 2 values
let secondLast = 0, last = hval[0];
// Compute current value using previous two values
// The final current value would be our result
let res;
for (let i = 1; i < n; i++) {
res = Math.max(hval[i] + secondLast, last);
secondLast = last;
last = res;
}
return res;
}
const hval = [6, 7, 1, 3, 8, 2, 4];
console.log(maxLoot(hval));
Time Complexity: O(n), Every value is computed only once.
Auxiliary Space: O(1), as we are using only two variables.
Find maximum possible stolen value - House Robber Problem
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem