-
Notifications
You must be signed in to change notification settings - Fork 0
/
q23_priority_queue_fast.cpp
49 lines (46 loc) · 1.24 KB
/
q23_priority_queue_fast.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
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
static auto x = [](){
std::ios::sync_with_stdio(false);
cin.tie(NULL);
return 0;
}();
class Solution {
public:
ListNode* mergeKLists(vector<ListNode*>& lists) {
ListNode* r=new ListNode(0),*end=r;
priority_queue<pair<ListNode*,int>,vector<pair<ListNode*,int>>,cmp> heap;
for(int i=0;i<lists.size();++i){
if(lists[i]){
heap.push({lists[i],i});
lists[i]=lists[i]->next;
}
}
while(!heap.empty()){
pair<ListNode*,int> temp;
temp=heap.top();
heap.pop();
end->next=temp.first;
end=temp.first;
if(lists[temp.second]){
heap.push({lists[temp.second],temp.second});
lists[temp.second]=lists[temp.second]->next;
}
}
//ListNode* clean=r;
//r=r->next;
//delete clean;
return r->next;
}
struct cmp{
bool operator()(pair<ListNode*,int> a,pair<ListNode*,int> b){
return a.first->val>b.first->val;
}
};
};