forked from amantyagi22/dynamic-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcellMitosis.cpp
More file actions
48 lines (43 loc) · 1.21 KB
/
cellMitosis.cpp
File metadata and controls
48 lines (43 loc) · 1.21 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
#include<iostream>
using namespace std;
/*I can comefrom n/2 --> n by doubling it
So the double state is we are calling is to half
and for increasing state we will call on decreasing state
so that we can come up to the required state by increasing it
Basically revesre engg. */
// Top Down approach
int cellProblem(int n,int x,int y,int z,int* dp){
if(n==0 || n==1) return 0;
if(dp[n]!=0){
return dp[n];
}
if(n%2==0){
int inc = cellProblem(n-1,x,y,z,dp) + y;
int dbl = cellProblem(n/2,x,y,z,dp) + x;
return dp[n] = min(inc,dbl);
}else{
int inc = cellProblem(n-1,x,y,z,dp) + y;
int dec_dbl = cellProblem((n+1)/2,x,y,z,dp) + z + x;
return dp[n] = min(inc,dec_dbl);
}
}
//Bottom Up approach
int cellProblemdp(int n,int x,int y,int z){
int dp[100] = {0};
dp[0] = dp[1] = 0;
for(int i=2;i<=n;i++){
if(i%2==0){
dp[i] = min(dp[i/2] + x,dp[i-1] + y);
}else{
dp[i] = min(dp[(i+1)/2]+z+x,dp[i-1]+y);
}
}
return dp[n];
}
int main(){
int n,x,y,z;
cin >> n >> x >> y >> z;
int dp[100] = {0};
cout << cellProblem(n,x,y,z,dp) << endl;
cout << cellProblemdp(n,x,y,z) << endl;
}