-
Notifications
You must be signed in to change notification settings - Fork 391
Expand file tree
/
Copy pathCountMinSteps_Iterative.py
More file actions
36 lines (29 loc) · 929 Bytes
/
CountMinSteps_Iterative.py
File metadata and controls
36 lines (29 loc) · 929 Bytes
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
# QUESTION : Given a positive integer 'n', find and return the minimum number of steps
# that 'n' has to take to get reduced to 1. You can perform
# any one of the following 3 steps:
# 1.) Subtract 1 from it. (n = n - 1) ,
# 2.) If n is divisible by 2, divide by 2.( if n % 2 == 0, then n = n / 2 ) ,
# 3.) If n is divisible by 3, divide by 3. (if n % 3 == 0, then n = n / 3 ).
# Now solve this iteratively
from sys import stdin
from sys import maxsize as MAX_VALUE
def countMinStepsToOne(n,dp) :
dp[0] = 0
dp[1] = 0
i = 2
while i<=n:
ans1 = dp[i-1]
ans2 = i-1
if i%2 == 0:
ans2 = dp[i//2]
ans3 = i-1
if i%3 == 0:
ans3 = dp[i//3]
ans = 1 + min(ans1,ans2,ans3)
dp[i] = ans
i += 1
return dp[n]
#main
n = int(stdin.readline().rstrip())
dp = [-1 for i in range(n+1)]
print(countMinStepsToOne(n,dp))