Python 129ms %99


  • 0
    A

    '''

    def magicalString(self, n):
        def translate(s):
            res = ''
            next = s[-1]
            for c in s:
                next = '2' if next == '1' else '1'
                res += next if c== '1' else next*2
            return res
    
        res = 0
        pre = '122112122122'
        count = len(pre)
        if count >= n:
            return pre[:n].count('1')
        res += pre.count('1') 
        n -= count
        s = '1121122'
        count = len(s)
        while count < n:
            res += s.count('1')
            n -= count
            s = translate(s)
            count = len(s)
    
        return res + s[:n].count('1')  
    

    '''


Log in to reply
 

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