Min Heap Construction Space : O(n) 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475#include <vector>using namespace std;// Do not edit the class below except for the buildHeap,// siftDown, siftUp, peek, remove, and insert methods.// Feel free to add new properties and methods to the class.class MinHeap {public: vector<int> heap; MinHeap(vector<int> vector) { // O(n logn) buildHeap(vector); } void buildHeap(vector<int> &vector) { for(auto& a : vector) insert(a); } void siftDown() { // O(logn) int i = 0; while(true) { int child1 = i * 2 + 1, child2 = i * 2 + 2; if(child1 >= heap.size()) return; if(child2 >= heap.size()) { if(heap[child1] < heap[i]) { swap(heap[child1], heap[i]); i = child1; } else return; } else { if(heap[child1] > heap[child2]) { if(heap[child2] < heap[i]) { swap(heap[child2], heap[i]); i = child2; } else return; } else { if(heap[child1] < heap[i]) { swap(heap[child1], heap[i]); i = child1; } else return; } } } } void siftUp() { // O(logn) int i = heap.size() - 1; while(i) { int par = (i - 1)/ 2; if(heap[par] > heap[i]) { swap(heap[par], heap[i]); i = par; } else return; } } int peek() { // O(1) if(heap.empty()) return -1; return heap[0]; } int remove() { // O(logn) int res = heap[0]; heap[0] = heap.back(); heap.pop_back(); siftDown(); return res; } void insert(int value) { // O(logn) heap.push_back(value); siftUp(); }};