-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDP-min steps to 1.cpp
70 lines (55 loc) · 1.01 KB
/
DP-min steps to 1.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
///this code is used for printing the min no. of steps used for going to a particular step in a staircase using DP
#include<iostream>
#include<climits>
using namespace std;
int c(int n,int*output)
{
int a=INT_MAX,b=INT_MAX,c=INT_MAX;
if(n<=1)
return 0;
if(n==2||n==3)
return 1;
output[0]=0;
output[1]=0;
output[2]=1;
output[3]=1;
for(int i=4;i<=n;i++)
{
if(i%3==0)
{
if(i%2==0)
{ a=output[i/2];
b=output[i/3];
c=output[i-1];
output[i]=min(a,min(a,b))+1;
}
else
{
b=output[i/3];
c=output[i-1];
output[i]=min(b,c)+1;
}
}
else if(i%2==0)
{ b=output[i/2];
c=output[i-1];
output[i]=min(b,c)+1;
}
else
output[i]=output[i-1]+1;
}
return output[n];
}
int countStepsTo1(int n){
int* output=new int[n+1];
for(int i=0;i<=n;i++)
{
output[i]=-1;
}
return c(n,output);
}
int main(){
int n;
cin >> n;
cout << countStepsTo1(n) << endl;
}