Implement Min Heap

Heaps Theory and Implementation Medium

You need to implement the Min Heap with the following given methods.

  • insert (x) -> insert value x to the min heap
  • getMin -> Output the minimum value from min heap
  • exctractMin -> Remove the minimum element from the heap
  • heapSize -> return the current size of the heap
  • isEmpty -> returns if heap is empty or not
  • changeKey (ind, val) -> update the value at given index to val (index will be given 0-based indexing)
  • initializeHeap -> Initialize the heap

Constraints

  • 1 <= n <= 105
  • -105 <= nums[i] <= 105

Hints

  • The heap is usually stored in an array. An empty list can represent an initial empty heap.
  • Add x to the end of the array. The smallest element is always at index 0. Swap the root (minimum element) with the last element. Return the length of the array representing the heap. Return True if the array is empty, otherwise False.

Company Tags

Epic Systems MongoDB Docker Swiggy Epic Games DoorDash Uber Boston Consulting Group Medtronic Morgan Stanley Bloomberg Freshworks GE Healthcare AMD Snowflake Twilio Reddit Johnson & Johnson Bain & Company American Express Instacart Lyft ARM Broadcom Roche Google Microsoft Amazon Meta Apple Netflix Adobe