forked from shuboc/LeetCode-2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
smallest-range.cpp
40 lines (34 loc) · 1.21 KB
/
smallest-range.cpp
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
29
30
31
32
33
34
35
36
37
38
39
// Time: O(nlogk)
// Space: O(k)
class Solution {
public:
vector<int> smallestRange(vector<vector<int>>& nums) {
using VIT = vector<int>::iterator;
const auto comp = [](const pair<VIT, VIT>& p1, const pair<VIT, VIT>& p2) {
return *p1.first > *p2.first;
};
int left = numeric_limits<int>::max(), right = numeric_limits<int>::min();
priority_queue<pair<VIT, VIT>, vector<pair<VIT, VIT>>, decltype(comp)> min_heap(comp);
for (auto &row : nums) {
left = min(left, row[0]);
right = max(right, row[0]);
min_heap.emplace(row.begin(), row.end());
}
vector<int> result = {left, right};
while (!min_heap.empty()) {
auto p = min_heap.top();
min_heap.pop();
++p.first;
if (p.first == p.second) {
break;
}
min_heap.emplace(p);
left = *min_heap.top().first;
right = max(right, *p.first);
if (right - left < result[1] - result[0]) {
result = {left, right};
}
}
return result;
}
};