-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmallestRangeCoveringElementsfromKLists.cpp
More file actions
35 lines (34 loc) · 1.13 KB
/
smallestRangeCoveringElementsfromKLists.cpp
File metadata and controls
35 lines (34 loc) · 1.13 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
35
// Source: https://leetcode.com/problems/smallest-range-covering-elements-from-k-lists/
// Author: Miao Zhang
// Date: 2021-02-25
class Solution {
public:
vector<int> smallestRange(vector<vector<int>>& nums) {
vector<pair<int, int>> vals;
int k = nums.size();
for (int i = 0; i < nums.size(); i++) {
for (auto num: nums[i]) {
vals.push_back({num, i});
}
}
sort(vals.begin(), vals.end());
unordered_map<int, int> dic;
int left = 0;
int cnt = 0;
int diff = INT_MAX;
vector<int> res;
for (int right = 0; right < vals.size(); right++) {
if (dic[vals[right].second] == 0) cnt++;
dic[vals[right].second]++;
while (cnt == k && left <= right) {
if (diff > vals[right].first - vals[left].first) {
diff = vals[right].first - vals[left].first;
res = {vals[left].first, vals[right].first};
}
if (--dic[vals[left].second] == 0) cnt--;
left++;
}
}
return res;
}
};