leetcode

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

1405.cpp (896B)


      1 class Solution {
      2     typedef tuple<int, char> record;
      3 
      4   public:
      5     string longestDiverseString(int a, int b, int c) const {
      6         priority_queue<record> pq;
      7         if (a > 0) pq.emplace(a, 'a');
      8         if (b > 0) pq.emplace(b, 'b');
      9         if (c > 0) pq.emplace(c, 'c');
     10 
     11         string res;
     12         int last = '!', lastc = 1;
     13         while (!pq.empty()) {
     14             const auto [cnt, c] = pq.top();
     15             pq.pop();
     16             if (c == last && lastc == 2) {
     17                 if (pq.empty()) return res;
     18                 const auto [ocnt, oc] = pq.top();
     19                 pq.pop();
     20                 if (ocnt > 1) pq.emplace(ocnt - 1, oc);
     21                 res += oc;
     22                 lastc = 0;
     23             }
     24             if (c != last) last = c, lastc = 0;
     25             if (cnt > 1) pq.emplace(cnt - 1, c);
     26             res += c;
     27             lastc++;
     28         }
     29         return res;
     30     }
     31 };