41. Heaps (striver sheet)
Topics-
1. check given array is heap
2. convert max to min heap
3. Kth smallest element
4. Kth largest element
5. Top K freq elements
Extras-
1. Quick Sort
2. Bucket Sort
3. Heap Sort
-------------------------------------------------------------------------------------------------------------------------
Heaps implementation using arrays-
Max heap Min Heap
------------------------------------------------------------
Heapify Algo : to place an element into its correct position in a binary heap
----------------------------------------------------------------------------------------------------------------------------
1. Check if array is heap or not-
ans: use true false in heapifyDown algo,
starting from idx 0 to last non leaf node
-----------------------------------------------------------------------------------------------------------------------------
2. Converting min to max heap:
apply heapifyDown algo from bottom to up.
start from first non leaf node
--------------------------------------------------------------------------------------------------------------------------
3. Kth largest & Kth smallest
done in love babbar notes
--------------------------------------------------------------------------------------------------------------------------
5. Top K freq elements
Brute Force- using map and sorting
Optimal- using min heap
we will build a min heap which sorts as per frequency
-----------------------------------------------------------------------------------------------------------------------------6. K-sorted or nearly sorted







Comments
Post a Comment