forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Merge_k_sorted_arrays.cpp
106 lines (87 loc) · 1.76 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
Merge K sorted arrays
=====================
Given K sorted arrays, merge all of them into a single sorted array
M-> Total number of elements
Time Complexity: O(MlogM)
Space Complexity: O(M)
*/
#include <iostream>
#include<queue>
using namespace std;
// Node class
class Node {
public:
int value; // Stores value of the node
int i; // Stores row number of the node
int j; // Stores column number of the node
// Constructor
Node(int value, int i, int j) {
this->value = value;
this->i = i;
this->j = j;
}
};
// Comparator class for priority_queue
class NodeCompare {
public:
bool operator()(Node a, Node b) {
return a.value > b.value;
}
};
// Merges k sorted arrays
void merge(vector<vector<int>> a, int k) {
// minHeap declared
priority_queue<Node, vector<Node>, NodeCompare> pq;
// pq must be initialised with the first elements of all the k arrays
for (int i = 0; i < k; i++) {
Node node(a[i][0], i, 0); // node is located at the ith row and 0th column
pq.push(node);
}
// Stores final array
vector<int> ans;
// While pq is not empty
while (pq.size()) {
// Consider the topmost element of pq
Node f = pq.top();
pq.pop();
// Topmost element must be stored in ans
ans.push_back(f.value);
// Push next element of same row into the pq
int row = f.i;
int col = f.j;
if (col + 1 < a[row].size()) {
Node new_node(a[row][col + 1], row, col + 1);
pq.push(new_node);
}
}
// Print ans
for (auto x : ans) {
cout << x << " ";
}
}
// driver code
int main() {
int k; cin >> k;
vector<vector<int>> a(k);
for (int i = 0; i < k; i++) {
int n; cin >> n;
for (int j = 0; j < n; j++) {
int x; cin >> x;
a[i].push_back(x);
}
}
merge(a, k);
}
/*
Input:
3
4
2 6 12 15
4
1 3 14 15
4
3 5 8 10
Output:
1 2 3 3 5 6 8 10 12 14 15 15
*/