forked from Ashish-kumar7/geeks-for-geeks-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmallest range in K lists.cpp
More file actions
41 lines (26 loc) · 1007 Bytes
/
Smallest range in K lists.cpp
File metadata and controls
41 lines (26 loc) · 1007 Bytes
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
class Solution{
public:
pair<int,int> findSmallestRange(int KSortedArray[][N], int n, int k)
{
//code here
pair<int,int>ans;
map<pair<int,int>,int>mp;
ans={0,INT_MAX};
for(int i=0;i<k;i++){
mp[{KSortedArray[i][0],i}]=0;
}
while(1){
int min_val=mp.begin()->first.first;
int max_val=mp.rbegin()->first.first;
if(ans.second-ans.first > max_val-min_val){
ans={min_val,max_val};
}
int row=mp.begin()->first.second;
int col=mp.begin()->second;
if(col==n-1)break;
mp.erase(mp.begin()->first);
mp[{KSortedArray[row][col+1],row}]=col+1;
}
return ans;
}
};