-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminimumStepsToOne_BottomUp.cpp
More file actions
49 lines (43 loc) · 1.3 KB
/
Copy pathminimumStepsToOne_BottomUp.cpp
File metadata and controls
49 lines (43 loc) · 1.3 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
#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;
int32_t main()
{
int n = 10;
vector<int> dp(100, INT_MAX);
dp[0] = 0, dp[1] = 0;
for(int i = 2 ; i <= n ; i++){
//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(i%3 == 0)
dp[i] = min(dp[i], dp[i/3] + 1);
if((i&1) == 0)
dp[i] = min(dp[i], dp[i/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(i%3 != 0 || (i&2) == 1)
dp[i] = min(dp[i], dp[i-1] + 1);
}
cout<<dp[n]<<endl;
}