#include
#include
#include
using namespace std;
typedef pair<int,pair<int,int>> node;
vector MergeKSortedArrays(vector<vector> v,int k)
{
vector result;
priority_queue<node,vector,greater> pq; // min priority_queue
// insert the 0th element of every array int the pq
for(int i=0;i<k;i++)
{
pq.push({v[i][0]},{i,0});
}
// remove the elements from minheap one bye one and add it to result vector
while(!pq.empty())
{
node current=pq.top();
pq.pop();
int element=current.first;
int x=current.second.first; // row in which element is present
int y=current.second.second; // coln in which element is present
result.push_back(element);
// push the element in pq after the current element i.e (x,y+1)
if(y+1<v[x].size())
pq.push({v[x][y+1]},{x,y+1});
}
return result;
}
int main()
{
vector<vector> v{{2,6,12,15},
{1,3,14,20},
{3,5,8,10}};
int k;
cin>>k;
vector<int> output=MergeKSortedArrays(v,k);
for(auto x:output)
{
cout<<x<<" ";
}
}
ERROR
main.cpp: In function ‘std::vector MergeKSortedArrays(std::vector<std::vector >, int)’:
main.cpp:17:32: error: no matching function for call to ‘std::priority_queue >, std::vector > >, std::greater > > >::push(, )’
17 | pq.push({v[i][0]},{i,0});
| ^
In file included from /usr/include/c++/9/queue:64,
from main.cpp:3:
/usr/include/c++/9/bits/stl_queue.h:627:7: note: candidate: ‘void std::priority_queue<_Tp, _Sequence, _Compare>::push(const value_type&) [with _Tp = std::pair >; _Sequence = std::vector > >; _Compare = std::greater > >; std::priority_queue<_Tp, _Sequence, _Compare>::value_type = std::pair >]’
627 | push(const value_type& __x)
| ^~~~
/usr/include/c++/9/bits/stl_queue.h:627:7: note: candidate expects 1 argument, 2 provided
/usr/include/c++/9/bits/stl_queue.h:635:7: note: candidate: ‘void std::priority_queue<_Tp, _Sequence, _Compare>::push(std::priority_queue<_Tp, _Sequence, _Compare>::value_type&&) [with _Tp = std::pair >; _Sequence = std::vector > >; _Compare = std::greater > >; std::priority_queue<_Tp, _Sequence, _Compare>::value_type = std::pair >]’
635 | push(value_type&& __x)
| ^~~~
/usr/include/c++/9/bits/stl_queue.h:635:7: note: candidate expects 1 argument, 2 provided
main.cpp:33:38: error: no matching function for call to ‘std::priority_queue >, std::vector > >, std::greater > > >::push(, )’
33 | pq.push({v[x][y+1]},{x,y+1});
| ^
In file included from /usr/include/c++/9/queue:64,
from main.cpp:3:
/usr/include/c++/9/bits/stl_queue.h:627:7: note: candidate: ‘void std::priority_queue<_Tp, _Sequence, _Compare>::push(const value_type&) [with _Tp = std::pair >; _Sequence = std::vector > >; _Compare = std::greater > >; std::priority_queue<_Tp, _Sequence, _Compare>::value_type = std::pair >]’
627 | push(const value_type& __x)
| ^~~~
/usr/include/c++/9/bits/stl_queue.h:627:7: note: candidate expects 1 argument, 2 provided
/usr/include/c++/9/bits/stl_queue.h:635:7: note: candidate: ‘void std::priority_queue<_Tp, _Sequence, _Compare>::push(std::priority_queue<_Tp, _Sequence, _Compare>::value_type&&) [with _Tp = std::pair >; _Sequence = std::vector > >; _Compare = std::greater > >; std::priority_queue<_Tp, _Sequence, _Compare>::value_type = std::pair >]’
635 | push(value_type&& __x)
| ^~~~
/usr/include/c++/9/bits/stl_queue.h:635:7: note: candidate expects 1 argument, 2 provided