-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminimumStepsToOne_TopDown.cpp
More file actions
59 lines (52 loc) · 1.69 KB
/
Copy pathminimumStepsToOne_TopDown.cpp
File metadata and controls
59 lines (52 loc) · 1.69 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
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <stack>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <sstream>
#include <iomanip>
using namespace std;
//Minimum steps to reach step one starting from n where we can take following steps -
// (1) n/2 (2) n/3 (3) n-1
vector<int> dp(100, INT_MAX);
int solve(int x)
{
//The base condition accounts for all those situtions where the user has reached step 1 or any invalid(negative) inputs where it would return 0
if (x <= 1)
return 0;
//We are memoizing here so as to avoid recursive calls if we have already calculated the value previously
if (dp[x] != INT_MAX)
return dp[x];
//Here we check for divisibility of step with 3 and 2 and if it is divisibile we reduce the step count and recursively call the function with adding 1
//for the step counted and storing the minimum on every step
if (x % 3 == 0)
dp[x] = min(dp[x], solve(x / 3) + 1);
if ((x & 1) == 0)
dp[x] = min(dp[x], solve(x / 2) + 1);
//Here we are trying to reduce the number of recurise calls by only taking step n-1 iff the number is was not divisible by 2 and 3 as in all the rest of scenarios
//we will hav step count reduced by n/2 or n/3 which would definetly be less than n-1;
if (x % 3 != 0 || (x & 1) == 1)
dp[x] = min(dp[x], solve(x - 1) + 1);
return dp[x];
}
int32_t main()
{
int n = 10;
cout << solve(n)<<endl;
}