-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge_k_sorted_arrays.cpp
66 lines (55 loc) · 1.37 KB
/
merge_k_sorted_arrays.cpp
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
/*
Given k sorted arrays of possibly different sizes, merge them and print the sorted output.
*/
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,pair<int,int>> ppi;
//time complexity O(nlogk);
vector<int> mergeKsorted(vector<vector<int>> a, int k){
priority_queue<ppi, vector<ppi>, greater<ppi>> pq;
vector<int> res;
//create a min heap and push 1st element of all array into the heap
for(int i = 0; i < k; i++){
pq.push(make_pair(a[i][0], make_pair(i,0)));
}
//repeat below 2 steps until pq becomees empty
//extract min element from the heap and store it in the result array
//push next element from the array from which the element is extracted. if there is no
//element in the array left then do nothing
while(!pq.empty()){
ppi cur = pq.top();
pq.pop();
res.push_back(cur.first);
int i = cur.second.first;
int j = cur.second.second;
if(j+1 < a[i].size()){
pq.push(make_pair(a[i][j+1], make_pair(i,j+1)));
}
}
return res;
}
int main(){
int k;
cin >> k;
vector<vector<int>> a(k);
for(int i = 0; i < k; i++){
int x;
while(1){
cin >> x;
if(x == -1) break;
a[i].push_back(x);
}
}
for(int i = 0; i < k; i++){
for(int j = 0; j < a[i].size(); j++){
cout << a[i][j] << " ";
}
cout << endl;
}
vector<int> res = mergeKsorted(a, k);
for(int i = 0; i < res.size(); i++){
cout << res[i] << " ";
}
cout << "\n";
return 0;
}