What is the difference between a min-heap and a max-heap?
LeetCode Patterns Flashcards: Heap and Priority Queue, Top K, Merge K Sorted
Аудио-карточка · 0:29Nortren·
What is the difference between a min-heap and a max-heap?
0:29
A min-heap places the smallest element at the root, so the parent is always smaller than or equal to its children. Extracting the minimum takes O of log n. A max-heap places the largest element at the root, so the parent is always larger than or equal to its children. Extracting the maximum takes O of log n. Most language libraries provide min-heaps by default. To simulate a max-heap, negate all values before insertion and negate them back when extracting. Choose min-heap when you need repeated access to the smallest element, and max-heap when you need the largest.
neetcode.io