C++ Solution O( root n) time. 0ms.


  • 0
    K
    class Solution {
    public:
        int minSteps(int n) {
            if(n==1) return 0;
            int steps = 0;
            int i;
            
            
            while(n%2 == 0){
                steps += 2;
                n /= 2;
            }
            
            for( i=3; i <= sqrt(n); i += 2){
                while(n%i == 0){
                    steps += i;
                    n /= i;
                }
                    
            }
            
            return (n != 1)?(steps + n):(steps);
        }
    };
    

Log in to reply
 

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