-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlootHouses.cpp
More file actions
92 lines (62 loc) · 1.52 KB
/
lootHouses.cpp
File metadata and controls
92 lines (62 loc) · 1.52 KB
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
// Loot Houses
// A thief wants to loot houses. He knows the amount of money in each house. He cannot loot two consecutive houses. Find the maximum amount of money he can loot.
// Input Format
// Line 1 : An integer N
// Line 2 : N spaced integers denoting money in each house
// Output Format
// Line 1 : Maximum amount of money looted
// Input Constraints
// 1 <= n <= 10^4
// 1 <= A[i] < 10^4
// Sample Input :
// 6
// 5 5 10 100 10 5
// Sample Output 1 :
// 110
#include <iostream>
using namespace std;
int getMaxMoney(int arr[], int n, int prevLooted, int *dp[]){
/*Write your code here.
*Don’t write main().
*Don’t take input, it is passed as function argument.
*Don’t print output.
*Taking input and printing output is handled automatically.
*/
if(n == 0) {
return 0;
}
if(dp[prevLooted][n] != -1) {
return dp[prevLooted][n];
}
int option1 = getMaxMoney(arr, n-1, 0, dp);
int option2 = 0;
if(!prevLooted) {
option2 = getMaxMoney(arr, n-1, 1, dp) + arr[n-1];
}
int ans = max(option1, option2);
dp[prevLooted][n] = ans;
return ans;
}
int getMaxMoney(int arr[], int n) {
// int *dp = new int[n+1];
int *dp[2];
dp[0] = new int[n+1];
dp[1] = new int[n+1];
for(int i = 0; i <= n; i++) {
dp[0][i] = -1;
dp[1][i] = -1;
}
int ans = getMaxMoney(arr, n, 0, dp);
delete [] dp[0];
delete [] dp[1];
return ans;
}
int main(){
int n;
cin >> n;
int arr[10000];
for(int i=0; i<n; i++){
cin >> arr[i];
}
cout << getMaxMoney(arr, n) << endl;
}