-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
sorted_arrays_merge.cc
47 lines (40 loc) · 1.25 KB
/
sorted_arrays_merge.cc
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
#include <algorithm>
#include <iterator>
#include <queue>
#include <vector>
#include "test_framework/generic_test.h"
using std::greater;
using std::next;
using std::priority_queue;
using std::vector;
vector<int> MergeSortedArrays(const vector<vector<int>>& sorted_arrays) {
struct IteratorCurrentAndEnd {
bool operator<(const IteratorCurrentAndEnd& that) const {
return *current > *that.current;
}
vector<int>::const_iterator current, end;
};
priority_queue<IteratorCurrentAndEnd> min_heap;
for (const vector<int>& sorted_array : sorted_arrays) {
if (!empty(sorted_array)) {
min_heap.push({cbegin(sorted_array), cend(sorted_array)});
}
}
vector<int> result;
while (!empty(min_heap)) {
auto [current, end] = min_heap.top();
min_heap.pop();
result.emplace_back(*current);
if (next(current) != end) {
min_heap.push({next(current), end});
}
}
return result;
}
int main(int argc, char* argv[]) {
std::vector<std::string> args{argv + 1, argv + argc};
std::vector<std::string> param_names{"sorted_arrays"};
return GenericTestMain(args, "sorted_arrays_merge.cc",
"sorted_arrays_merge.tsv", &MergeSortedArrays,
DefaultComparator{}, param_names);
}