-
Notifications
You must be signed in to change notification settings - Fork 0
/
632.hpp
74 lines (62 loc) · 1.67 KB
/
632.hpp
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//
// Created by bai on 17-7-2.
//
#ifndef LEETCODE_632_HPP
#define LEETCODE_632_HPP
#include <iostream>
#include <queue>
#include <algorithm>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <numeric>
using namespace std;
struct vinr {
int value;
int ridx;
bool operator>(const vinr &rhs) const {
return value > rhs.value;
}
vinr(int value, int ridx) : value(value), ridx(ridx) {}
};
class Solution {
public:
vector<int> smallestRange(vector<vector<int>> &nums) {
priority_queue<vinr, vector<vinr>, greater<vinr>> priq;
int left = numeric_limits<int>::max(), right = numeric_limits<int>::min();
for (int i = 0; i < nums.size(); ++i) {
priq.push(vinr(nums[i].front(), i));
if (nums[i].front() > right) {
right = nums[i].front();
}
if (nums[i].front() < left) {
left = nums[i].front();
}
}
vector<int> idxs(nums.size(), 1);
int minRange = right - left;
int l = left, r = right;
while (true) {
auto idx = priq.top().ridx;
priq.pop();
if (idxs[idx] >= nums[idx].size()) {
break;
}
int v = nums[idx][idxs[idx]];
priq.push(vinr(v, idx));
idxs[idx]++;
if (v > right) {
right = v;
}
left = priq.top().value;
if (right - left < minRange) {
l = left;
r = right;
minRange = r - l;
}
}
return {l, r};
}
};
#endif //LEETCODE_632_HPP