Heaps and priority queue in Java
Important class
PriorityQueue<Integer> minHeap = new PriorityQueue<>();
PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder());Problems to practice
- Kth largest element
- Top K frequent elements
- Merge K sorted lists
- Find median in stream
Example: min heap
PriorityQueue<Integer> pq = new PriorityQueue<>();
pq.offer(5);
pq.offer(3);
pq.offer(9);
System.out.println(pq.peek()); // 3Time and space complexity
- Insert: O(log n)
- Peek: O(1)
- Remove min: O(log n)
Can you explain it without the notes?
Heaps and priority queue in Java
Use your own words. Explain an example and one limitation before checking the explanation.
Check your explanation
Important class
Turn this concept into a clear answer.
Practice this conceptAdapted from 15_DSA_JAVA.md · “8. Heaps and priority queue in Java”. Examples are study fragments; framework behavior can depend on your version.