Heap (Priority Queue) LeetCode Problems: All 163, With Python Solutions
Every problem in this library that LeetCode tags Heap (Priority Queue) — 163 in total, 138 of them with a complete Python solution, a worked example and the time and space complexity of the approach.
- 163 problems
- 13 Easy
- 87 Medium
- 63 Hard
How Heap (Priority Queue) problems are solved
A tag names the subject, not the method. These pattern hubs cover the techniques that actually solve Heap (Priority Queue) problems — each one explains the approach, gives a Python template and states its complexity.
- Heap / Priority Queue — Keep only the best k elements, or always pull the smallest, in log time.
Heap (Priority Queue) problems by difficulty
Problems with a complete Python solution are listed first, then by ascending problem number.
Easy (13)
| # | Problem | Difficulty | Topics |
|---|---|---|---|
| 506 | Relative Ranks | Easy | Array, Sorting, Heap (Priority Queue) |
| 703 | Kth Largest Element in a Stream | Easy | Tree, Design, Binary Search Tree +3 |
| 1046 | Last Stone Weight | Easy | Array, Heap (Priority Queue) |
| 1337 | The K Weakest Rows in a Matrix | Easy | Array, Binary Search, Matrix +2 |
| 1464 | Maximum Product of Two Elements in an Array | Easy | Array, Sorting, Heap (Priority Queue) |
| 2099 | Find Subsequence of Length K With the Largest Sum | Easy | Array, Hash Table, Sorting +1 |
| 2231 | Largest Number After Digit Swaps by Parity | Easy | Sorting, Heap (Priority Queue) |
| 2335 | Minimum Amount of Time to Fill Cups | Easy | Greedy, Array, Sorting +1 |
| 2357 | Make Array Zero by Subtracting Equal Amounts | Easy | Greedy, Array, Hash Table +3 |
| 2500 | Delete Greatest Value in Each Row | Easy | Array, Matrix, Sorting +2 |
| 2558 | Take Gifts From the Richest Pile | Easy | Array, Simulation, Heap (Priority Queue) |
| 2974 | Minimum Number Game | Easy | Array, Sorting, Simulation +1 |
| 1086 | High FivePremium | Easy | Array, Hash Table, Sorting +1 |
Medium (87)
| # | Problem | Difficulty | Topics |
|---|---|---|---|
| 215 | Kth Largest Element in an Array | Medium | Array, Divide and Conquer, Quickselect +2 |
| 264 | Ugly Number II | Medium | Hash Table, Math, Dynamic Programming +1 |
| 347 | Top K Frequent Elements | Medium | Array, Hash Table, Divide and Conquer +5 |
| 355 | Design Twitter | Medium | Design, Hash Table, Linked List +1 |
| 373 | Find K Pairs with Smallest Sums | Medium | Array, Heap (Priority Queue) |
| 378 | Kth Smallest Element in a Sorted Matrix | Medium | Array, Binary Search, Matrix +2 |
| 451 | Sort Characters By Frequency | Medium | Hash Table, String, Bucket Sort +3 |
| 621 | Task Scheduler | Medium | Greedy, Array, Hash Table +3 |
| 658 | Find K Closest Elements | Medium | Array, Two Pointers, Binary Search +3 |
| 659 | Split Array into Consecutive Subsequences | Medium | Greedy, Array, Hash Table +1 |
| 692 | Top K Frequent Words | Medium | Trie, Array, Hash Table +5 |
| 743 | Network Delay Time | Medium | Depth-First Search, Breadth-First Search, Graph +2 |
| 767 | Reorganize String | Medium | Greedy, Hash Table, String +3 |
| 786 | K-th Smallest Prime Fraction | Medium | Array, Two Pointers, Binary Search +2 |
| 787 | Cheapest Flights Within K Stops | Medium | Depth-First Search, Breadth-First Search, Graph +3 |
| 855 | Exam Room | Medium | Design, Ordered Set, Heap (Priority Queue) |
| 912 | Sort an Array | Medium | Array, Divide and Conquer, Bucket Sort +5 |
| 973 | K Closest Points to Origin | Medium | Geometry, Array, Math +4 |
| 1054 | Distant Barcodes | Medium | Greedy, Array, Hash Table +3 |
| 1094 | Car Pooling | Medium | Array, Prefix Sum, Sorting +2 |
| 1268 | Search Suggestions System | Medium | Trie, Array, String +3 |
| 1338 | Reduce Array Size to The Half | Medium | Greedy, Array, Hash Table +2 |
| 1353 | Maximum Number of Events That Can Be Attended | Medium | Greedy, Array, Sorting +1 |
| 1405 | Longest Happy String | Medium | Greedy, String, Heap (Priority Queue) |
| 1424 | Diagonal Traverse II | Medium | Array, Sorting, Heap (Priority Queue) |
| 1438 | Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit | Medium | Queue, Array, Ordered Set +3 |
| 1488 | Avoid Flood in The City | Medium | Greedy, Array, Hash Table +2 |
| 1514 | Path with Maximum Probability | Medium | Graph, Array, Shortest Path +1 |
| 1631 | Path With Minimum Effort | Medium | Depth-First Search, Breadth-First Search, Union Find +4 |
| 1642 | Furthest Building You Can Reach | Medium | Greedy, Array, Heap (Priority Queue) |
| 1648 | Sell Diminishing-Valued Colored Balls | Medium | Greedy, Array, Math +3 |
| 1686 | Stone Game VI | Medium | Greedy, Array, Math +3 |
| 1696 | Jump Game VI | Medium | Queue, Array, Dynamic Programming +2 |
| 1705 | Maximum Number of Eaten Apples | Medium | Greedy, Array, Heap (Priority Queue) |
| 1738 | Find Kth Largest XOR Coordinate Value | Medium | Bit Manipulation, Array, Divide and Conquer +5 |
| 1753 | Maximum Score From Removing Stones | Medium | Greedy, Math, Heap (Priority Queue) |
| 1786 | Number of Restricted Paths From First to Last Node | Medium | Graph, Topological Sort, Dynamic Programming +2 |
| 1792 | Maximum Average Pass Ratio | Medium | Greedy, Array, Heap (Priority Queue) |
| 1801 | Number of Orders in the Backlog | Medium | Array, Simulation, Heap (Priority Queue) |
| 1834 | Single-Threaded CPU | Medium | Array, Sorting, Heap (Priority Queue) |
| 1845 | Seat Reservation Manager | Medium | Design, Heap (Priority Queue) |
| 1878 | Get Biggest Three Rhombus Sums in a Grid | Medium | Array, Math, Matrix +3 |
| 1882 | Process Tasks Using Servers | Medium | Array, Heap (Priority Queue) |
| 1942 | The Number of the Smallest Unoccupied Chair | Medium | Array, Hash Table, Heap (Priority Queue) |
| 1962 | Remove Stones to Minimize the Total | Medium | Greedy, Array, Heap (Priority Queue) |
| 1985 | Find the Kth Largest Integer in the Array | Medium | Array, String, Divide and Conquer +3 |
| 2034 | Stock Price Fluctuation | Medium | Design, Hash Table, Data Stream +2 |
| 2054 | Two Best Non-Overlapping Events | Medium | Array, Binary Search, Dynamic Programming +2 |
| 2146 | K Highest Ranked Items Within a Price Range | Medium | Breadth-First Search, Array, Matrix +2 |
| 2182 | Construct String With Repeat Limit | Medium | Greedy, Hash Table, String +2 |
| 2208 | Minimum Operations to Halve Array Sum | Medium | Greedy, Array, Heap (Priority Queue) |
| 2233 | Maximum Product After K Increments | Medium | Greedy, Array, Heap (Priority Queue) |
| 2285 | Maximum Total Importance of Roads | Medium | Greedy, Graph, Sorting +1 |
| 2333 | Minimum Sum of Squared Difference | Medium | Greedy, Array, Binary Search +2 |
| 2336 | Smallest Number in Infinite Set | Medium | Design, Hash Table, Ordered Set +1 |
| 2342 | Max Sum of a Pair With Equal Sum of Digits | Medium | Array, Hash Table, Sorting +1 |
| 2343 | Query Kth Smallest Trimmed Number | Medium | Array, String, Divide and Conquer +4 |
| 2349 | Design a Number Container System | Medium | Design, Hash Table, Ordered Set +1 |
| 2353 | Design a Food Rating System | Medium | Design, Array, Hash Table +3 |
| 2406 | Divide Intervals Into Minimum Number of Groups | Medium | Greedy, Array, Two Pointers +3 |
| 2424 | Longest Uploaded Prefix | Medium | Union Find, Design, Binary Indexed Tree +5 |
| 2456 | Most Popular Video Creator | Medium | Array, Hash Table, String +2 |
| 2462 | Total Cost to Hire K Workers | Medium | Array, Two Pointers, Simulation +1 |
| 2497 | Maximum Star Sum of a Graph | Medium | Greedy, Graph, Array +2 |
| 2512 | Reward Top K Students | Medium | Array, Hash Table, String +2 |
| 2530 | Maximal Score After Applying K Operations | Medium | Greedy, Array, Heap (Priority Queue) |
| 2542 | Maximum Subsequence Score | Medium | Greedy, Array, Sorting +1 |
| 2593 | Find Score of an Array After Marking All Elements | Medium | Array, Hash Table, Sorting +2 |
| 2611 | Mice and Cheese | Medium | Greedy, Array, Sorting +1 |
| 2662 | Minimum Cost of a Path With Special Roads | Medium | Graph, Array, Shortest Path +1 |
| 2679 | Sum in a Matrix | Medium | Array, Matrix, Sorting +2 |
| 2762 | Continuous Subarrays | Medium | Queue, Array, Ordered Set +3 |
| 2812 | Find the Safest Path in a Grid | Medium | Breadth-First Search, Union Find, Array +3 |
| 2944 | Minimum Number of Coins for Fruits | Medium | Queue, Array, Dynamic Programming +2 |
| 253 | Meeting Rooms IIPremium | Medium | Greedy, Array, Two Pointers +3 |
| 505 | The Maze IIPremium | Medium | Depth-First Search, Breadth-First Search, Graph +4 |
| 1057 | Campus BikesPremium | Medium | Array, Sorting, Heap (Priority Queue) |
| 1102 | Path With Maximum Minimum ValuePremium | Medium | Depth-First Search, Breadth-First Search, Union Find +4 |
| 1135 | Connecting Cities With Minimum CostPremium | Medium | Union Find, Graph, Minimum Spanning Tree +1 |
| 1167 | Minimum Cost to Connect SticksPremium | Medium | Greedy, Array, Heap (Priority Queue) |
| 1500 | Design a File Sharing SystemPremium | Medium | Design, Hash Table, Data Stream +2 |
| 1810 | Minimum Path Cost in a Hidden GridPremium | Medium | Depth-First Search, Breadth-First Search, Graph +5 |
| 2015 | Average Height of Buildings in Each SegmentPremium | Medium | Greedy, Array, Sorting +1 |
| 2093 | Minimum Cost to Reach City With DiscountsPremium | Medium | Graph, Shortest Path, Heap (Priority Queue) |
| 2473 | Minimum Cost to Buy ApplesPremium | Medium | Graph, Array, Shortest Path +1 |
| 2599 | Make the Prefix Sum Non-negativePremium | Medium | Greedy, Array, Heap (Priority Queue) |
| 2737 | Find the Closest Marked NodePremium | Medium | Graph, Array, Shortest Path +1 |
Hard (63)
| # | Problem | Difficulty | Topics |
|---|---|---|---|
| 23 | Merge k Sorted Lists | Hard | Linked List, Divide and Conquer, Heap (Priority Queue) +1 |
| 218 | The Skyline Problem | Hard | Binary Indexed Tree, Segment Tree, Array +5 |
| 239 | Sliding Window Maximum | Hard | Queue, Array, Sliding Window +2 |
| 295 | Find Median from Data Stream | Hard | Design, Two Pointers, Data Stream +2 |
| 407 | Trapping Rain Water II | Hard | Breadth-First Search, Array, Matrix +1 |
| 420 | Strong Password Checker | Hard | Greedy, String, Heap (Priority Queue) |
| 480 | Sliding Window Median | Hard | Array, Hash Table, Sliding Window +1 |
| 502 | IPO | Hard | Greedy, Array, Sorting +1 |
| 630 | Course Schedule III | Hard | Greedy, Array, Sorting +1 |
| 632 | Smallest Range Covering Elements from K Lists | Hard | Greedy, Array, Hash Table +3 |
| 675 | Cut Off Trees for Golf Event | Hard | Breadth-First Search, Array, Matrix +1 |
| 778 | Swim in Rising Water | Hard | Depth-First Search, Breadth-First Search, Union Find +4 |
| 857 | Minimum Cost to Hire K Workers | Hard | Greedy, Array, Sorting +1 |
| 862 | Shortest Subarray with Sum at Least K | Hard | Queue, Array, Binary Search +4 |
| 871 | Minimum Number of Refueling Stops | Hard | Greedy, Array, Dynamic Programming +1 |
| 882 | Reachable Nodes In Subdivided Graph | Hard | Graph, Shortest Path, Heap (Priority Queue) |
| 1172 | Dinner Plate Stacks | Hard | Stack, Design, Hash Table +1 |
| 1263 | Minimum Moves to Move a Box to Their Target Location | Hard | Breadth-First Search, Array, Matrix +1 |
| 1354 | Construct Target Array With Multiple Sums | Hard | Array, Heap (Priority Queue) |
| 1368 | Minimum Cost to Make at Least One Valid Path in a Grid | Hard | Breadth-First Search, Graph, Array +3 |
| 1383 | Maximum Performance of a Team | Hard | Greedy, Array, Sorting +1 |
| 1388 | Pizza With 3n Slices | Hard | Greedy, Array, Dynamic Programming +1 |
| 1425 | Constrained Subsequence Sum | Hard | Queue, Array, Dynamic Programming +3 |
| 1439 | Find the Kth Smallest Sum of a Matrix With Sorted Rows | Hard | Array, Binary Search, Matrix +1 |
| 1499 | Max Value of Equation | Hard | Queue, Array, Sliding Window +2 |
| 1606 | Find Servers That Handled Most Number of Requests | Hard | Array, Ordered Set, Simulation +1 |
| 1675 | Minimize Deviation in Array | Hard | Greedy, Array, Ordered Set +1 |
| 1687 | Delivering Boxes from Storage to Ports | Hard | Segment Tree, Queue, Array +4 |
| 1776 | Car Fleet II | Hard | Stack, Array, Math +2 |
| 1825 | Finding MK Average | Hard | Design, Queue, Data Stream +2 |
| 1851 | Minimum Interval to Include Each Query | Hard | Array, Binary Search, Sorting +2 |
| 1912 | Design Movie Rental System | Hard | Design, Array, Hash Table +2 |
| 2102 | Sequentially Ordinal Rank Tracker | Hard | Design, Data Stream, Ordered Set +1 |
| 2163 | Minimum Difference in Sums After Removal of Elements | Hard | Array, Dynamic Programming, Heap (Priority Queue) |
| 2290 | Minimum Obstacle Removal to Reach Corner | Hard | Breadth-First Search, Graph, Array +3 |
| 2344 | Minimum Deletions to Make Array Divisible | Hard | Array, Math, Number Theory +2 |
| 2386 | Find the K-Sum of an Array | Hard | Array, Sorting, Heap (Priority Queue) |
| 2398 | Maximum Number of Robots Within Budget | Hard | Queue, Array, Binary Search +4 |
| 2402 | Meeting Rooms III | Hard | Array, Hash Table, Sorting +2 |
| 2454 | Next Greater Element IV | Hard | Stack, Array, Binary Search +3 |
| 2503 | Maximum Number of Points From Grid Queries | Hard | Breadth-First Search, Union Find, Array +4 |
| 2532 | Time to Cross a Bridge | Hard | Array, Simulation, Heap (Priority Queue) |
| 2551 | Put Marbles in Bags | Hard | Greedy, Array, Sorting +1 |
| 2577 | Minimum Time to Visit a Cell In a Grid | Hard | Breadth-First Search, Graph, Array +3 |
| 2617 | Minimum Number of Visited Cells in a Grid | Hard | Stack, Breadth-First Search, Union Find +5 |
| 2642 | Design Graph With Shortest Path Calculator | Hard | Graph, Design, Shortest Path +1 |
| 2699 | Modify Graph Edge Weights | Hard | Graph, Shortest Path, Heap (Priority Queue) |
| 2813 | Maximum Elegance of a K-Length Subsequence | Hard | Stack, Greedy, Array +3 |
| 2931 | Maximum Spending After Buying Items | Hard | Greedy, Array, Matrix +2 |
| 2940 | Find Building Where Alice and Bob Can Meet | Hard | Stack, Binary Indexed Tree, Segment Tree +4 |
| 2959 | Number of Possible Sets of Closing Branches | Hard | Bit Manipulation, Graph, Enumeration +2 |
| 2973 | Find Number of Coins to Place in Tree Nodes | Hard | Tree, Depth-First Search, Dynamic Programming +2 |
| 272 | Closest Binary Search Tree Value IIPremium | Hard | Stack, Tree, Depth-First Search +4 |
| 358 | Rearrange String k Distance ApartPremium | Hard | Greedy, Hash Table, String +3 |
| 499 | The Maze IIIPremium | Hard | Depth-First Search, Breadth-First Search, Graph +5 |
| 642 | Design Search Autocomplete SystemPremium | Hard | Depth-First Search, Design, Trie +4 |
| 683 | K Empty SlotsPremium | Hard | Binary Indexed Tree, Segment Tree, Queue +5 |
| 759 | Employee Free TimePremium | Hard | Array, Sorting, Line Sweep +1 |
| 1168 | Optimize Water Distribution in a VillagePremium | Hard | Union Find, Graph, Minimum Spanning Tree +1 |
| 1183 | Maximum Number of OnesPremium | Hard | Greedy, Math, Sorting +1 |
| 1199 | Minimum Time to Build BlocksPremium | Hard | Greedy, Array, Math +1 |
| 2714 | Find Shortest Path with K HopsPremium | Hard | Graph, Shortest Path, Heap (Priority Queue) |
| 2969 | Minimum Number of Coins for Fruits IIPremium | Hard | Queue, Array, Dynamic Programming +2 |
Keep exploring
- Array1,569
- String672
- Hash Table588
- Math485
- Dynamic Programming481
- Sorting392
- Greedy346
- Depth-First Search289
- Binary Search253
- Database249
- Tree225
- Breadth-First Search223
- Matrix216
- Two Pointers201
- Bit Manipulation194
- Binary Tree174
- Prefix Sum157
- Stack157
- Simulation144
- Graph138
- Counting126
- Design122
- Sliding Window116
- Backtracking105
When the Heap (Priority Queue) problem arrives live
Stealth Interview is a desktop app for macOS and Windows. It reads the coding problem off your screen, returns a working solution with a step-by-step explanation and its time and space complexity, and transcribes what the interviewer is saying — while staying invisible to screen sharing.