Find maximum possible stolen value from houses in C++

C++Server Side ProgrammingProgramming

In this problem, we are given n houses with some values in them. Our task is to Find the maximum possible stolen value from houses.

Problem Description − We have an array houses[] that consist of the values that are in each house. A thief robs the houses but he cannot steal from two adjacent houses as neighbours know about the theft. We need to find the maximum possible value that can be stolen by the thief from the house without getting caught.

Let’s take an example to understand the problem,

Input

houses[] = {5, 2, 1, 6, 7, 9, 4, 3}

Output

23

Explanation

The max values can be stolen as : 5, 6, 9, 3.

Solution Approach

The solution of the problem can be found using dynamic programming. As we need to find the maximum stolen value by the thief such that if he steals from a house at index i, then he cannot steal from the house at index (i+1). Also, we would not have stolen from the house at index (i-1). To solve the problem, we will create an DP array of size n. And for base case we will initialize the DP[0] with houses[0] and DP[1] with houses[1]. Then, we will find all the values of DP from index 2 to n-1. The value of DP[i] will be the maximum value out of DP[i-2] + houses[i] or DP[i-1]. And at the end the last value of the DP array is the maximum value that can be stolen.

Program to illustrate the working of our solution,

Example

 Live Demo

#include <iostream>
using namespace std;
int calMax(int a, int b){
   if(a > b)
      return a;
      return b;
}
int findMaxValuesStolen(int houses[], int n) {
   if (n == 0)
      return 0;
   int DP[n];
   DP[0] = houses[0];
   DP[1] = calMax(houses[0], houses[1]);
   for (int i = 2; i<n; i++)
      DP[i] = calMax( (houses[i] + DP[i-2]), DP[i-1]);
   return DP[n-1];
}
int main() {
   int houses[] = {5, 2, 1, 6, 7, 9, 4, 3};
   int n = sizeof(houses)/sizeof(houses[0]);
   cout<<"The maximum possible values stolen from the houses is
   "<<findMaxValuesStolen(houses, n);
   return 0;
}

Output

The maximum possible values stolen from the houses is 23

This solution is good but it can be made more efficient using the fact that the maximum values stolen can be found using only two values. As in the DP, we have used only two values for each index. So, we can use only two variable to find the solution which is decrease the space complexity to the problem,

Program to illustrate the working of our solution,

Example

 Live Demo

#include <iostream>
using namespace std;
int calMax(int a, int b){
   if(a > b)
      return a;
      return b;
}
int findMaxValuesStolen(int houses[], int n) {
   if (n == 0)
      return 0;
   int maxValStolen;
   int val1 = houses[0];
   int val2 = calMax(houses[0], houses[1]);
   for (int i = 2; i<n; i++) {
      maxValStolen = calMax( (houses[i]+val1) , val2);
      val1 = val2;
      val2 = maxValStolen;
   }
   return maxValStolen;
}
int main() {
   int houses[] = {5, 2, 1, 6, 7, 9, 4, 3};
   int n = sizeof(houses)/sizeof(houses[0]);
   cout<<"The maximum possible values stolen from the houses is "<<findMaxValuesStolen(houses, n);
   return 0;
}

Output

The maximum possible values stolen from the houses is 23
raja
Published on 12-Mar-2021 05:56:10
Advertisements