-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHouseRober.cpp
More file actions
66 lines (57 loc) · 1.28 KB
/
HouseRober.cpp
File metadata and controls
66 lines (57 loc) · 1.28 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
#include <iostream>
#include <vector>
#include <cstring>
#include <climits>
using namespace std;
// Recursive
int robHouse(int *arr,int n){
if(n < 0 || n == 0) return 0;
int ans = max(arr[n-1] + robHouse(arr,n-2),robHouse(arr,n-1));
return ans;
}
// TDDP
int rob2(int *arr,int n,int *dp){
if(n < 0 || n == 0) return 0;
if(dp[n] > 0) return dp[n];
int ans = max(arr[n-1] + rob2(arr,n-2,dp),rob2(arr,n-1,dp));
return dp[n] = ans;
}
// BUDP
int robbDP(int *arr,int n){
int *dp = new int[n+1];
dp[0] = 0;
for(int i=1;i<=n;i++){
dp[i] = max(arr[i-1] + dp[i-2], dp[i-1]);
}
memset(dp,-1,sizeof(dp));
int ans = dp[n];
delete [] dp;
return ans;
}
int main()
{
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
int *dp = new int[n+1];
for(int i=0;i<=n;i++){
dp[i] = 0;
}
cout<<robHouse(arr,n)<<endl;
cout<<rob2(arr,n,dp)<<endl;
cout<<robbDP(arr,n);
delete [] dp;
return 0;
}
// |---------------------------------------------------|-
// | input1 |
// | 5 |
// | 2 7 9 3 1 |
// | |
// | input 2 |
// | 4 |
// | 1 2 3 1 |
// |---------------------------------------------------|-