-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q4_HouseRobber.cpp
103 lines (79 loc) · 2.47 KB
/
Q4_HouseRobber.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include<iostream>
#include<vector>
using namespace std;
int recursiveSol(vector<int>&houes , int n){
//base cases
if(n <0){
return 0;
}
if(n ==0 ){
return houes[0]; //houses[0] ki jagah houses[n] likh dega (fasega)
}
//the include exlude
int include = recursiveSol(houes, n-2) + houes[n];
int exclude = recursiveSol(houes, n-1) + 0;
return max(include,exclude);
}
//top down approch optimising the time complexity
int topDownApproch(vector<int>&houses,int numberOfHouse, vector<int>dpArray ){
//base cases
if(numberOfHouse <0){
return 0;
}
if(numberOfHouse == 0 ){
return houses[0]; //houses[0] ki jagah houses[n] likh dega (fasega)
}
//step3: check if the house is robbed or not
if(dpArray [numberOfHouse] != -1){
return dpArray[numberOfHouse];
}
//the include exlude
int include = recursiveSol(houses, numberOfHouse-2) + houses[numberOfHouse];
int exclude = recursiveSol(houses, numberOfHouse-1) + 0;
// step2 store the ans in dp array
dpArray[numberOfHouse] = max(include,exclude);
return dpArray[numberOfHouse];
}
int bottomUp(vector<int>&houses ,int n){
//creating the dparry
vector<int>dp(n+1, 0);
dp[0] = houses[0];
for(int i =1;i<=n ;i++){
int temp =0;
if(i-2>=0){
temp = dp[i-2];
}
int include = temp + houses[i];
int exclude = dp[i-1] + 0;
dp[i] = max(include,exclude);
}
return dp[n];
}
int spaceOptimisation(vector<int>&houses , int n){
int prev2 = 0;
int prev1 = houses[0];
int curr = 0;
for(int i =1;i<=n;i++){
int temp = 0;
if(i-2>=0){
temp = prev2;
}
int include = temp +houses[i];
int exclude = prev1 +0;
curr= max(include , exclude);
prev2 =prev1;
prev1 = curr;
}
return curr;
}
int main(){
// vector<int>houses{2,7,9,3,1};
vector<int>houses{1,2,3,1};
int numberOfHouses = houses.size()-1; //size +1 mein fasega
// int ans = recursiveSol(houses , numberOfHouses);
// step 1: creating the dp aarrya
vector<int>dp(numberOfHouses+1, -1 );
int ans = spaceOptimisation(houses , numberOfHouses );
cout<<"Maximum Amount can be robbed is: "<<ans<<endl;
return 0;
}