Merge K sorted arrays
The first line of input contains an integer, that denotes value of k.
The following lines of input represent k sorted arrays. Each sorted array uses two lines of input. The first line contains an integer, that denotes the size of the array. The following line contains elements of the array.
The first and only line of output contains space separated elements of the merged and sorted array, as described in the task.
0 <= k <= 1000
0 <= n1, n2 <= 10000
Time Limit: 1 second
4
3
1 5 9
2
45 90
5
2 6 78 100 234
1
0
0 1 2 5 6 9 45 78 90 100 234
#include<bits/stdc++.h>vector<int> mergeKSortedArrays(vector<vector<int>*> input) { // Write your code here priority_queue<int,vector<int>,greater<int>>minh; for(int i=0;i<input.size();i++){ for(int j=0;j<input[i]->size();j++){ minh.push(input[i]->at(j)); } }
vector<int>ans; while(!minh.empty()){ ans.push_back(minh.top()); minh.pop(); }
return ans;}
Comments
Post a Comment