leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
0481.cpp (499B)
0 class Solution {
1 public:
2 int magicalString(const int n) const {
3 if (n <= 3) return 1;
5 queue<int> q;
6 q.push(2);
8 int count = 2, res = 1, last = 2;
9 while (!q.empty()) {
10 const int crnt = q.front();
11 q.pop();
13 res += crnt == 1;
14 if (++count == n) return res;
16 last = last == 1 ? 2 : 1;
17 for (int i = 0; i < crnt; i++)
18 q.push(last);
19 }
21 return -1;
22 }
23 };