How to implement minimum step to one using topDown approach using C#?

The minimum steps to one problem asks: given a positive integer n, find the minimum number of steps to reduce it to 1 using these operations −

  • If n is divisible by 3, divide it by 3

  • If n is divisible by 2, divide it by 2

  • Subtract 1 from n

The top-down approach uses recursion with memoization to solve this problem efficiently by storing computed results in a DP array.

How It Works

The algorithm recursively explores all possible operations at each step and chooses the one that gives the minimum result. Memoization prevents redundant calculations by storing results in the dp array.

Top-Down Approach for n=10 10 5 ÷2 9 -1 unavailable ÷3 Choose minimum among available operations Memoize result to avoid recomputation

Syntax

Following is the syntax for the top-down dynamic programming approach −

public int MinSteps(int n, int[] dp) {
    if (n == 1) return 0;
    
    int op1 = int.MaxValue, op2 = int.MaxValue, op3 = int.MaxValue;
    
    if (n % 3 == 0) op1 = MinSteps(n / 3, dp);
    if (n % 2 == 0) op2 = MinSteps(n / 2, dp);
    op3 = MinSteps(n - 1, dp);
    
    return dp[n] = Math.Min(Math.Min(op1, op2), op3) + 1;
}

Example

Here's a complete implementation that finds the minimum steps to reduce a number to 1 −

using System;

public class DynamicProgramming {
    public int MinimumStepsToOneTopDown(int n, int[] dp) {
        if (n == 1) {
            return 0;
        }
        
        if (dp[n] != 0) {
            return dp[n];
        }
        
        int op1 = int.MaxValue;
        int op2 = int.MaxValue; 
        int op3 = int.MaxValue;
        
        if (n % 3 == 0) {
            op1 = MinimumStepsToOneTopDown(n / 3, dp);
        }
        
        if (n % 2 == 0) {
            op2 = MinimumStepsToOneTopDown(n / 2, dp);
        }
        
        op3 = MinimumStepsToOneTopDown(n - 1, dp);
        
        int ans = Math.Min(Math.Min(op1, op2), op3) + 1;
        return dp[n] = ans;
    }
}

class Program {
    static void Main(string[] args) {
        DynamicProgramming solver = new DynamicProgramming();
        int[] dpArr = new int[150];
        
        int result = solver.MinimumStepsToOneTopDown(10, dpArr);
        Console.WriteLine("Minimum steps to reduce 10 to 1: " + result);
        
        int[] dpArr2 = new int[50];
        result = solver.MinimumStepsToOneTopDown(15, dpArr2);
        Console.WriteLine("Minimum steps to reduce 15 to 1: " + result);
    }
}

The output of the above code is −

Minimum steps to reduce 10 to 1: 3
Minimum steps to reduce 15 to 1: 4

Step-by-Step Trace

For n = 10, the algorithm works as follows −

  • Step 1: 10 ? 5 (divide by 2) or 10 ? 9 (subtract 1)

  • Step 2: From 5 ? 4 (subtract 1), from 9 ? 3 (divide by 3)

  • Step 3: Continue until reaching 1

The optimal path is: 10 ? 9 ? 3 ? 1 (3 steps total).

Performance Analysis

Complexity Value Description
Time Complexity O(N) Each subproblem is solved once due to memoization
Space Complexity O(N) DP array and recursion stack space

Conclusion

The top-down approach with memoization efficiently solves the minimum steps to one problem by exploring all valid operations and choosing the optimal path. The DP array prevents redundant calculations, reducing time complexity from exponential to linear O(N).

Updated on: 2026-03-17T07:04:36+05:30

196 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements