Simple Java recursive solution


  • 0
    U
    public int minSteps(int n) {
            if (n == 1) {
                return 0;
            }
            
            int res = n;
            for (int i = 2; i < n; i++) {
                if (n % i == 0) {
                    res = i + minSteps(n/i);
                    break;
                }
            }
    
            return res;
        }
    

  • 0
    U

    I think the time complexity is less than O(n), maybe O(log(n)).


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.