Mar 29 · 14 min read · TLDR: To find the top K largest elements, maintain a min-heap of size K. For every new element, push it onto the heap. If the heap exceeds K, evict the minimum. After processing all N elements, the he
Join discussionMar 29 · 15 min read · TLDR: Two Heaps partitions a stream into two sorted halves. A max-heap holds everything below the median; a min-heap holds everything above it. Keep the heaps size-balanced and you can read the median
Join discussionDec 10, 2024 · 6 min read · Welcome to Day 2 of my 100 Days of DSA challenge! Today, I solved five problems focused on arrays. Here's a quick look at the questions I tackled and how I approached them 🤖✨ Check out my GitHub repository for all my solutions and progress. Let’s ke...
Join discussionOct 16, 2024 · 4 min read · Key Takeaways Learn what heaps are and their fundamental properties Master both Max Heap and Min Heap implementations Understand heap operations with O(log n) time complexity Implement heaps in Python and JavaScript Apply heaps to solve real-wor...
Join discussion
Sep 4, 2024 · 19 min read · Introduction to Priority Queues and Heaps Introduction to Priority Queues (PQ) A Priority Queue (PQ) is a specialized type of queue where each element is assigned a priority. Elements with higher priority are processed before those with lower priorit...
Join discussion
Jul 11, 2024 · 5 min read · To see the question, click here. Naive Approach The idea is to store the elements in a list. At each stage of adding an element, we will sort the list because the elements may not be added to the list in a sorted fashion. // TC: O(nlogn) for addNum a...
Join discussionJul 9, 2024 · 2 min read · In Java, heaps are typically implemented using the PriorityQueue class, which is part of the java.util package. A heap is a specialized tree-based data structure that satisfies the heap property. The PriorityQueue class provides an implementation of ...
Join discussionJun 25, 2024 · 2 min read · Two Heaps is a pattern used to solve problems that involve maintaining a dynamic set of elements with the ability to find the median, the smallest or largest elements efficiently, or to balance two sets of elements. This pattern typically involves us...
Join discussion