MinimumStepstoOneBottomdownApproachtakes integer n as input. Parameter n contains the total number of elements. Initial condition checks whether n is equal to 1. If n is equal to 1 then return 0. Initialize op1, op2 and op3 to max value. If n mod 3 is equal to 0 then call MinimumStepstoOneBottomdownApproach recursively and assign it to op1, if n mod 3 is equal to 0 then call MinimumStepstoOneBottomdownApproach recursively and assign it to op2 else subtract n by 1 and call MinimumStepstoOneBottomdownApproach. Finally return the value from the dp array
Time complexity − O(N)
Space complexity − O(N)
Example
public class DynamicProgramming{
public int MinimumStepstoOneBottomdownApproach(int n){
int[] dp = new int[100];
dp[1] = 0;
for (int i = 2; i < n; i++){
int op1 = int.MaxValue, op2 = int.MaxValue, op3 = int.MaxValue;
if (n % 3 == 0){
op1 = dp[i / 3];
}
if (n % 2 == 0){
op2 = dp[i / 2];
}
op3= dp[i -1];
dp[i]= Math.Min(Math.Min(op1, op2), op3) + 1;
}
return dp[n-1];
}
}
static void Main(string[] args){
DynamicProgramming dp = new DynamicProgramming();
Console.WriteLine(dp.MinimumStepstoOneBottomdownApproach(10))
}Output
3