-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmallest_Number_Range.cpp
More file actions
84 lines (63 loc) · 2.17 KB
/
Smallest_Number_Range.cpp
File metadata and controls
84 lines (63 loc) · 2.17 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
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
75
76
77
78
79
80
81
82
83
84
/*
Given ‘M’ sorted arrays, find the smallest range that includes at least one number from each of the ‘M’ lists.
Example 1:
Input: L1=[1, 5, 8], L2=[4, 12], L3=[7, 8, 10]
Output: [4, 7]
Explanation: The range [4, 7] includes 5 from L1, 4 from L2 and 7 from L3.
Example 2:
Input: L1=[1, 9], L2=[4, 12], L3=[7, 10, 16]
Output: [9, 12]
Explanation: The range [9, 12] includes 9 from L1, 12 from L2 and 10 from L3.
*/
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,pair<int,int>> ppi;
pair<int,int> Find_smallest_Range(vector<vector<int>> &lists)
{
priority_queue<ppi,vector<ppi>,greater<ppi>> minheap;
int Range_Start=0;
int Range_End= numeric_limits<int>::max();
int MaxCurrentNum=numeric_limits<int>::min();
for(int i=0;i<lists.size();i++)
{
if(!lists[i].empty())
{
minheap.push({lists[i][0],{i,0}});
MaxCurrentNum=max(MaxCurrentNum,lists[i][0]);
}
}
while(minheap.size()==lists.size())
{
ppi node=minheap.top();
minheap.pop();
if(Range_End-Range_Start > MaxCurrentNum-node.first)
{
Range_Start=node.first;
Range_End=MaxCurrentNum;
}
node.second.second++;
if(lists[node.second.first].size()>node.second.second)
{
node.first=lists[node.second.first][node.second.second];
minheap.push(node);
MaxCurrentNum=max(MaxCurrentNum,node.first);
}
}
return make_pair(Range_Start,Range_End);
}
int main()
{
vector<vector<int>> lists={{1,5,8},{4,12},{7,8,10}};
auto result=Find_smallest_Range(lists);
cout<<result.first<<endl;
cout<<result.second<<endl;
}
/* Time Complexity : O(N * Log M)
we’ll be going through all the elements of all the arrays and will
remove/add one element in the heap in each step, the time complexity
of the above algorithm will be O(N*logM)O(N∗logM) where ‘N’ is the total
number of elements in all the ‘M’ input arrays.
Space Complexity: The space complexity will be O(M)
because, at any time, our min-heap will be store one number from all the ‘M’ input arrays.
*/