-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathProblem2.cpp
More file actions
49 lines (35 loc) · 899 Bytes
/
Problem2.cpp
File metadata and controls
49 lines (35 loc) · 899 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//DESIGN A MIN HEAP
class minHeap{
vector<int> h;
public:
minHeap() {}
int getMin(){
return h[0];
}
void insert(int x){
h.push_back(x);
int i = h.size() - 1;
while(i > 0 && (i-1)/2 > i){
swap(h[(i-1)/2],h[i]);
i = i - 1/2;//root
}
}
int extractMin(){
int minVal = h[0];
h[0] = h.push_back();
h.pop_back();
int i = 0;
int n = h.size();
while(true){
int l = 2*i + 1;//left
int r = 2*i + 2;//right
int s = i;
if (l < n && h[l] < h[s]) s = l;//left child exists
if (r < n && h[r] < h[s]) s = r;//right child exists
if (s == i) break;//parent is already smaller than both children
swap(h[i], h[s]);
i = s;
}
return minVal;
}
};