Python simple case based solution, beats 92.14% solutions, with comments


  • 0
    A
    import math
    class Solution(object):
        def minSteps(self, n):
            """
            :type n: int
            :rtype: int
            """
            # base case
            if n == 1:
                return 0
            # even
            elif not n%2:
                return self.minSteps(n/2)+2
            # prime odd or non-prime odd
            else:
                for i in range(3,int(math.ceil(math.sqrt(n))+1),2):
                    if not n%i:
                        return self.minSteps(i) + self.minSteps(n/i)
                return n
    

Log in to reply
 

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