-
Notifications
You must be signed in to change notification settings - Fork 13
/
CS_52_MergeKSortedArrays.cpp
64 lines (50 loc) · 1.61 KB
/
CS_52_MergeKSortedArrays.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
#include <bits/stdc++.h>
using namespace std;
/*
Time Complexity: O((N * K) * log(K))
Space Complexity: O(N * K)
Where N is the total number of elements in all the arrays, and K is the
number of arrays.
*/
#include <queue>
vector<int> mergeKSortedArrays(vector<vector<int>> &kArrays, int k)
{
vector<int> result;
// Create a min heap to store atmost k heap nodes at a time.
priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>, greater<pair<int, pair<int, int>>>>
minHeap;
for (int i = 0; i < kArrays.size(); i++)
{
minHeap.push({kArrays[i][0], {i, 0}});
}
while (minHeap.empty() == false)
{
// Remove the minimum element from the heap.
pair<int, pair<int, int>> curr = minHeap.top();
minHeap.pop();
// i is the array number and j is the index of the removed element in the
// ith array.
int i = curr.second.first;
int j = curr.second.second;
// Add the removed element to the output array.
result.push_back(curr.first);
// If the next element of the extracted element exists, add it to the heap.
if (j + 1 < kArrays[i].size())
{
minHeap.push({kArrays[i][j + 1], {i, j + 1}});
}
}
// Return the output array.
return result;
}
int main()
{
vector<vector<int>> kArrays = {{2, 6, 12, 34}, {1, 9, 20, 1000}, {23, 34, 90, 2000}};
vector<int> result = mergeKSortedArrays(kArrays, kArrays.size());
for (int i = 0; i < result.size(); i++)
{
cout << result[i] << " ";
}
cout << endl;
return 0;
}