slug: heap-application title: Applications of Heap tags: [data structure, heap]
leetcode natively supports JS PQs, including a Min one.
github datastructures - priority queue
github leetcode - environment for the programming languages
- The Top K Problem
- --
Use the Heap data structure to obtain Top K’s largest or smallest elements.
Solution of the Top K largest elements:
- Construct a Max Heap.
- Add all elements into the Max Heap.
- Traversing and deleting the top element, and store the value into the result arry T.
- Repeat step 3 until we have removed the K largest elements.
Solution of the Top K smalllest elements
- Construct a Min Heap
- Add all elements into the Min Heap
- Traversing and deleing the top element, and store the value into the result array T.
- Repeat step 3 until we have removed the K smallest elements.
Time Complexity: O(KlogN + N) Space complexity: O(N)
hello orange