leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0481.cpp (499B)


      1 class Solution {
      2   public:
      3     int magicalString(const int n) const {
      4         if (n <= 3) return 1;
      5 
      6         queue<int> q;
      7         q.push(2);
      8 
      9         int count = 2, res = 1, last = 2;
     10         while (!q.empty()) {
     11             const int crnt = q.front();
     12             q.pop();
     13 
     14             res += crnt == 1;
     15             if (++count == n) return res;
     16 
     17             last = last == 1 ? 2 : 1;
     18             for (int i = 0; i < crnt; i++)
     19                 q.push(last);
     20         }
     21 
     22         return -1;
     23     }
     24 };