While solving DSA problems one of the important steps is to find which data structure will be used. Lets focus when we should use heap data structure but before that we will see what are heaps
Heap :
A heap is a specialized tree-based data structure that satisfied the heap property:
if B is a child node of A, then key(A) ≥ key(B). This implies that an element with the greatest key is always in the root node, and so such a heap is sometimes called a max-heap and
the opposite relation would be min heap.
Heaps are structures meant to allow quick access to the min or the max.
Use :
Heaps are used when we need the highest or lowest order/priority element removed/fetched. They allow quick access to this item in Constant O(1) time.
You can use a minHeap or maxHeap when you want to access the smallest and largest elements respectively
It maintains data semiordered, Therefore it is a good tradeoff between the cost of maintaining a complete order and the cost of searching through random chaos