-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0632.cpp
More file actions
28 lines (23 loc) · 802 Bytes
/
Copy path0632.cpp
File metadata and controls
28 lines (23 loc) · 802 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Solution {
public:
vector<int> smallestRange(const vector<vector<int>> &nums) const {
using ti = tuple<int, int, int>;
priority_queue<ti, vector<ti>, greater<ti>> pq;
int maxi = -1;
for (int i = 0; i < size(nums); i++) {
pq.emplace(nums[i][0], i, 0);
maxi = max(maxi, nums[i][0]);
}
vector<int> res = {0, INT_MAX};
while (!pq.empty()) {
const auto [mini, list, elem] = pq.top();
pq.pop();
if (maxi - mini < res[1] - res[0]) res = {mini, maxi};
if (elem + 1 == size(nums[list])) break;
const int next = nums[list][elem + 1];
pq.emplace(next, list, elem + 1);
maxi = max(maxi, next);
}
return res;
}
};