C++ straight forward method O(n)


  • 0
    J
    class Solution {
    public:
        int magicalString(int n) {
            if (n <= 3)
                return n > 0;
            queue<int> q;
            int res = 1;
            bool one = true;
            q.push(2);
            n -= 3; //122, start from second 2
            while (n) {
                int cnt = q.front();
                q.pop();
                while (n && cnt--) {
                    q.push(one ? 1 : 2);
                    n--;
                    res += one;
                }
                one = !one;
            }
            return res;
        }
    };
    

Log in to reply
 

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