File tree Expand file tree Collapse file tree 1 file changed +38
-0
lines changed
19 - Heap Data Structure Problems/15 - Smallest Range Covering Element from K Lists Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+ public:
3+ vector<int > smallestRange (vector<vector<int >>& nums) {
4+ int k = nums.size ();
5+ vector<int > ptr (k, 0 );
6+
7+ int minRange = INT_MAX;
8+ int start = -1 ;
9+ int end = -1 ;
10+
11+ while (true ){
12+ int mini = INT_MAX;
13+ int maxi = INT_MIN;
14+ int minRow = -1 ;
15+
16+ for (int i = 0 ; i < k; i++){
17+ int value = nums[i][ptr[i]];
18+
19+ if (value < mini){
20+ mini = value;
21+ minRow = i;
22+ }
23+ maxi = max (maxi, value);
24+ }
25+
26+ if (maxi - mini < minRange){
27+ minRange = maxi - mini;
28+ start = mini;
29+ end = maxi;
30+ }
31+
32+ ptr[minRow]++;
33+ if (ptr[minRow] == nums[minRow].size ()) break ;
34+ }
35+
36+ return {start, end};
37+ }
38+ };
You can’t perform that action at this time.
0 commit comments