-
Notifications
You must be signed in to change notification settings - Fork 231
Expand file tree
/
Copy pathMergeTwoBinaryMaxHeaps.cpp
More file actions
32 lines (27 loc) · 901 Bytes
/
MergeTwoBinaryMaxHeaps.cpp
File metadata and controls
32 lines (27 loc) · 901 Bytes
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
// https://practice.geeksforgeeks.org/problems/merge-two-binary-max-heap0144/1
class Solution{
public:
void heapify(vector<int> &arr, int i, int size){
int largest= i;
int left= 2*i+1;
int right= 2*i+2;
if(left<size && arr[largest]<arr[left])
largest= left;
if(right<size && arr[largest]<arr[right])
largest= right;
if(largest!=i){
swap(arr[largest], arr[i]);
heapify(arr, largest, size);
}
}
vector<int> mergeHeaps(vector<int> &a, vector<int> &b, int n, int m) {
// your code here
vector<int> merged;
merged.insert(merged.end(), a.begin(), a.end());
merged.insert(merged.end(), b.begin(), b.end());
for(int i= (n+m)/2-1; i>=0; i--){
heapify(merged, i, n+m);
}
return merged;
}
};