leetcode

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

0632.cpp (802B)


      1 class Solution {
      2   public:
      3     vector<int> smallestRange(const vector<vector<int>> &nums) const {
      4         using ti = tuple<int, int, int>;
      5         priority_queue<ti, vector<ti>, greater<ti>> pq;
      6         int maxi = -1;
      7 
      8         for (int i = 0; i < size(nums); i++) {
      9             pq.emplace(nums[i][0], i, 0);
     10             maxi = max(maxi, nums[i][0]);
     11         }
     12 
     13         vector<int> res = {0, INT_MAX};
     14         while (!pq.empty()) {
     15             const auto [mini, list, elem] = pq.top();
     16             pq.pop();
     17 
     18             if (maxi - mini < res[1] - res[0]) res = {mini, maxi};
     19             if (elem + 1 == size(nums[list])) break;
     20 
     21             const int next = nums[list][elem + 1];
     22             pq.emplace(next, list, elem + 1);
     23             maxi = max(maxi, next);
     24         }
     25 
     26         return res;
     27     }
     28 };