Heaps

47 articles

dsa2 min read

Last Stone Weight

Simulate stone smashing by repeatedly extracting the two heaviest stones using a max-heap.

Read →
dsa2 min read

Relative Ranks

Assign gold/silver/bronze or rank numbers to athletes based on their scores using sorted ordering.

Read →
dsa3 min read

Find K Closest Elements

Find K closest elements to x in a sorted array using binary search to find the optimal left boundary.

Read →
dsa2 min read

Top K Frequent Elements

Return the K most frequent elements using a frequency map plus min-heap, or bucket sort for O(n) solution.

Read →
dsa1 min read

Sort Characters By Frequency

Sort characters in a string by decreasing frequency using a frequency counter and max-heap or bucket sort.

Read →
dsa2 min read

K Closest Points to Origin

Find K points closest to the origin using a max-heap of size K or quickselect for O(n) average.

Read →
dsa2 min read

Task Scheduler

Find minimum CPU intervals to schedule tasks with cooldown using a greedy max-heap simulation.

Read →
dsa3 min read

Reorganize String

Rearrange a string so no two adjacent characters are the same using a greedy max-heap approach.

Read →
dsa2 min read

Find Median from Data Stream

Maintain a dynamic median using two heaps: a max-heap for the lower half and a min-heap for the upper half.

Read →
dsa3 min read

Sliding Window Median

Compute medians of all sliding windows using two heaps with lazy deletion for element removal.

Read →
dsa2 min read

Merge K Sorted Lists

Merge K sorted linked lists into one sorted list using a min-heap to always extract the globally smallest node.

Read →
dsa2 min read

Find K Pairs with Smallest Sums

Find K pairs (u, v) with smallest sums from two sorted arrays using a min-heap initialized with first row candidates.

Read →
dsa2 min read

Ugly Number II

Find the nth ugly number (only prime factors 2, 3, 5) using three pointers for the 2x, 3x, 5x merge streams.

Read →
dsa2 min read

Course Schedule III

Maximize courses taken within deadlines by greedily scheduling and swapping out the longest past course.

Read →
dsa2 min read

Meeting Rooms II

Find minimum conference rooms required by tracking room end times with a min-heap of ongoing meeting endings.

Read →
dsa2 min read

IPO

Maximize capital after k IPO investments by greedily picking the highest profit project among affordable ones.

Read →
dsa2 min read

Car Pooling

Check if a car can pick up all passengers by processing start/end events with a sorted timeline or heap.

Read →
dsa2 min read

Furthest Building You Can Reach

Greedily use ladders for the largest climbs and bricks for smaller gaps, managed with a min-heap of ladder sizes.

Read →
dsa3 min read

Process Tasks Using Servers

Assign tasks to available servers using two heaps: one for free servers and one for busy servers sorted by free time.

Read →
dsa3 min read

Trapping Rain Water II

Calculate trapped water in a 3D height map using BFS with a min-heap to process cells from shortest boundary inward.

Read →
dsa3 min read

Swim in Rising Water

Find minimum time to swim from top-left to bottom-right in a grid where time = max elevation on the path.

Read →
dsa2 min read

Super Ugly Number

Find the nth super ugly number with prime factors only from a given list using K-pointer dynamic programming.

Read →
dsa2 min read

Minimum Number of Refueling Stops

Find minimum fuel stops to reach the target by greedily selecting the largest available fuel stations when running low.

Read →
dsa3 min read

Design Twitter

Design a simplified Twitter with follow/unfollow and a news feed showing the 10 most recent tweets from followed users.

Read →
dsa2 min read

Ugly Number III

Find the nth number divisible by a, b, or c using binary search with inclusion-exclusion counting formula.

Read →
dsa2 min read

Maximum Performance of a Team

Maximize team performance (sum of speeds * min efficiency) by sorting by efficiency and using a min-heap on speeds.

Read →
dsa2 min read

Longest Happy String

Build the longest string with at most 2 consecutive identical characters by always using the most frequent available character.

Read →
dsa2 min read

Maximum Frequency Stack

Design a stack that pops the most frequent element, breaking ties by most recently pushed, using frequency-bucket stacks.

Read →
dsa2 min read

Minimize Deviation in Array

Minimize max-min of an array by doubling odd numbers and repeatedly halving the max using a max-heap.

Read →
dsa1 min read

Find Right Interval

For each interval find the interval with the smallest start point >= the end point using sorted starts and binary search.

Read →
dsa1 min read

Kth Largest Sum in a Binary Tree

Find the kth largest level sum in a binary tree using BFS to compute level sums then sorting or using a heap.

Read →
dsa2 min read

Total Cost to Hire K Workers

Minimize total hiring cost by always choosing the cheapest candidate from the first or last p candidates using two min-heaps.

Read →
dsa1 min read

Seat Reservation Manager

Design a seat manager that reserves the lowest numbered available seat and unreserves seats using a min-heap.

Read →
dsa1 min read

Stock Price Fluctuation

Track stock prices with corrections by maintaining a sorted multiset and a timestamp-to-price map.

Read →
dsa1 min read

Reduce Array Size to At Least Half

Find the minimum number of elements to remove to reduce array size by half by greedily removing most frequent elements.

Read →