Python solution by factorization of n


  • 0
    Z
    class Solution(object):
        def minSteps(self, n):
            """
            :type n: int
            :rtype: int
            """
            def factorization(n):
                factor_dict = {}
                start = 2
                while n>=start:
                    while n%start!=0:
                        start += 1
                    count = 1
                    n = n//start
                    if start in factor_dict:
                        factor_dict[start]+=1
                    else:
                        factor_dict[start]=1
                return factor_dict
            if n==1:
                return 0
            factors_dict = factorization(n)
            ret = 0
            for key in factors_dict:
                ret += key * factors_dict[key]
            return ret
    '''

Log in to reply
 

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