-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminStepsBU.cpp
46 lines (39 loc) · 985 Bytes
/
minStepsBU.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
#include <bits/stdc++.h>
using namespace std;
int minSteps(int n)
{
int dp [100];
dp[0] = 0;
for(int i=1;i<=n;i++)
{
int temp =0 ;
if( (i+1)%3 ==0)
{
if((i+1)%2 ==0)
{
temp= min(dp[((i+1)/3)-1],dp[((i+1)/2)-1]);
dp[i]=min(temp,dp[i-1])+1;
}
else
dp[i]= min(dp[((i+1)/3)-1],dp[(i-1)])+1;
}
else
{
if((i+1)%2 ==0)
{
dp[i] = min(dp[((i+1)/2)-1],dp[i-1])+1;
}
else
dp[i]= dp[(i-1)]+1;
}
}
return dp[n-1];
}
int main()
{
int x;
cin>>x;
int res = minSteps(x);
cout<<"\n"<<res;
return 0;
}