Magical String LeetCode solution in Python
1# Runtime: 489 ms (Top 6.14%) | Memory: 14.1 MB (Top 72.07%)2class Solution:3def magicalString(self, n: int) -> int:4queue, ans, i = deque([2]), 1, 156while i <= n - 2:7m = queue.popleft()8ans += m == 19queue.extend([1 + (i % 2 == 0)] * m)10i += 11112return ans