Straightforward Python solution


  • 0
    class Solution(object):
        def magicalString(self, n):
            if n < 2: return n
            count = 1
            a = [1]*n
            a[1] = 2
            i, j = 1, 1
            while j < n:
                for k in range(min(a[i], n-j)):
                    if a[j-1] == 1:
                        a[j+k] = 2
                    else:
                        a[j+k] = 1
                if a[j] == 1:
                    count += min(a[i], n-j)
                j += a[i]
                i += 1
            return count
    

Log in to reply
 

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