python,recursive,8 lines with most lines explained


  • 0
    Q
    class Solution(object):
        def minSteps(self, n):
            if n == 1:
                return 0
            elif n & 1 == 0: # n is even
                return 2 + self.minSteps(n >> 1) # copy once + past once + minsteps to generate "(n/2) 'A'"
            else: # n is odd
                for i in range(3, n + 1, 2): # find minimial i which is dividable to n so that to (i - 1) times to make n 'A'. 
                    if n % i == 0:
                        return i + self.minSteps(n / i) # copy once + past (i-1) times of "(n/i) 'A'" + minsteps to generate "(n/i) 'A'"
    

Log in to reply
 

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