-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path632.cpp
More file actions
34 lines (34 loc) · 1.16 KB
/
632.cpp
File metadata and controls
34 lines (34 loc) · 1.16 KB
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
class Solution {
public:
vector<int> smallestRange(vector<vector<int>>& nums) {
vector<pair<int, int>> numsWIdx;
int m = nums.size();
for (int i = 0; i < m; ++i) {
for (auto& num : nums[i]) {
numsWIdx.push_back(make_pair(num, i));
}
}
sort(numsWIdx.begin(), numsWIdx.end());
vector<int> counts(m, 0);
int left = 0;
int n = numsWIdx.size();
int numOfList = 0;
int minInterval = INT_MAX;
vector<int> res;
for (int right = 0; right < n; ++right) {
if (counts[numsWIdx[right].second] == 0) numOfList++;
counts[numsWIdx[right].second]++;
while (numOfList == m) {
int interval = numsWIdx[right].first - numsWIdx[left].first + 1;
if (interval < minInterval) {
minInterval = interval;
res = {numsWIdx[left].first, numsWIdx[right].first};
}
counts[numsWIdx[left].second]--;
if (counts[numsWIdx[left].second] == 0) numOfList--;
left++;
}
}
return res;
}
};