-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path104.h
More file actions
128 lines (120 loc) · 3.32 KB
/
104.h
File metadata and controls
128 lines (120 loc) · 3.32 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/**
* Definition of ListNode
* class ListNode {
* public:
* int val;
* ListNode *next;
* ListNode(int val) {
* this->val = val;
* this->next = NULL;
* }
* }
*/
#define LEFTNODE(i) ((i) * 2 + 1)
#define RIGHTNODE(i) ((i) * 2 + 2)
#define PARENTNODE(i) ((i) > 0 ? (((i) - 1) / 2) : -1)
// class MinHeap
class MinHeap{
public:
MinHeap(const vector<ListNode*> &arr){
//copy(arr.begin(), arr.end(), back_inserter(array));
count = arr.size();
array = new ListNode*[count];
copy(arr.begin(), arr.end(), array);
int i = PARENTNODE(count - 1);
while(i >= 0){
shiftDown(i);
i--;
}
}
~MinHeap(){
if(array) delete[] array;
}
int size() const { return count; }
bool empty() const { return count == 0; }
ListNode* extractMin(){
ListNode* r = array[0];
swap(array[0], array[count-1]);
--count;
shiftDown(0);
return r;
}
void insert(ListNode* r){
array[count] = r;
++count;
shiftUp(count - 1);
}
private:
ListNode** array;
int count;
void shiftDown(int i){
while(LEFTNODE(i) < count){
int j = LEFTNODE(i);
if(RIGHTNODE(i) < count && array[j]->val > array[RIGHTNODE(i)]->val){
j = RIGHTNODE(i);
}
if(array[i]->val < array[j]->val) break; // !!
swap(array[i], array[j]);
cout << array[i]->val << " " << array[j]->val << endl;
i = j;
}
}
void shiftUp(int i){
while(i > 0 && array[PARENTNODE(i)]->val > array[i]->val){
swap(array[PARENTNODE(i)], array[i]);
i = PARENTNODE(i);
}
}
};
class Solution {
public:
/**
* @param lists: a list of ListNode
* @return: The head of one sorted list.
*/
ListNode *mergeKLists(vector<ListNode *> &lists) {
// write your code here
// ListNode *head = nullptr;
// for(int i = 0; i < lists.size(); i++){
// head = merge(head, lists[i]);
// }
// return head;
ListNode dummyhead(-1);
ListNode *head = &dummyhead;
vector<ListNode*> arr;
for(int i = 0; i < lists.size(); i++){
if(lists[i] == nullptr) continue;
arr.push_back(lists[i]);
}
MinHeap heap(arr);
while(!heap.empty()){
ListNode* node = heap.extractMin();
head->next = node;
head = head->next;
if(node->next != nullptr){
heap.insert(node->next);
}
}
head->next = nullptr;
return dummyhead.next;
}
ListNode *merge(ListNode *list1, ListNode *list2){
ListNode dummy(0);
ListNode *dummyhead = &dummy;
while(list1 != nullptr && list2 != nullptr){
if(list1->val < list2->val){
dummyhead->next = list1;
dummyhead = list1;
list1 = list1->next;
}
else{
dummyhead->next = list2;
dummyhead = list2;
list2 = list2->next;
}
}
if(list1) dummyhead->next = list1;
else dummyhead->next = list2;
return dummy.next;
}
};