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) problems by difficulty

Problems with a complete Python solution are listed first, then by ascending problem number.

Easy (13)

#ProblemDifficultyTopics
506Relative RanksEasyArray, Sorting, Heap (Priority Queue)
703Kth Largest Element in a StreamEasyTree, Design, Binary Search Tree +3
1046Last Stone WeightEasyArray, Heap (Priority Queue)
1337The K Weakest Rows in a MatrixEasyArray, Binary Search, Matrix +2
1464Maximum Product of Two Elements in an ArrayEasyArray, Sorting, Heap (Priority Queue)
2099Find Subsequence of Length K With the Largest SumEasyArray, Hash Table, Sorting +1
2231Largest Number After Digit Swaps by ParityEasySorting, Heap (Priority Queue)
2335Minimum Amount of Time to Fill CupsEasyGreedy, Array, Sorting +1
2357Make Array Zero by Subtracting Equal AmountsEasyGreedy, Array, Hash Table +3
2500Delete Greatest Value in Each RowEasyArray, Matrix, Sorting +2
2558Take Gifts From the Richest PileEasyArray, Simulation, Heap (Priority Queue)
2974Minimum Number GameEasyArray, Sorting, Simulation +1
1086High FivePremiumEasyArray, Hash Table, Sorting +1

Medium (87)

#ProblemDifficultyTopics
215Kth Largest Element in an ArrayMediumArray, Divide and Conquer, Quickselect +2
264Ugly Number IIMediumHash Table, Math, Dynamic Programming +1
347Top K Frequent ElementsMediumArray, Hash Table, Divide and Conquer +5
355Design TwitterMediumDesign, Hash Table, Linked List +1
373Find K Pairs with Smallest SumsMediumArray, Heap (Priority Queue)
378Kth Smallest Element in a Sorted MatrixMediumArray, Binary Search, Matrix +2
451Sort Characters By FrequencyMediumHash Table, String, Bucket Sort +3
621Task SchedulerMediumGreedy, Array, Hash Table +3
658Find K Closest ElementsMediumArray, Two Pointers, Binary Search +3
659Split Array into Consecutive SubsequencesMediumGreedy, Array, Hash Table +1
692Top K Frequent WordsMediumTrie, Array, Hash Table +5
743Network Delay TimeMediumDepth-First Search, Breadth-First Search, Graph +2
767Reorganize StringMediumGreedy, Hash Table, String +3
786K-th Smallest Prime FractionMediumArray, Two Pointers, Binary Search +2
787Cheapest Flights Within K StopsMediumDepth-First Search, Breadth-First Search, Graph +3
855Exam RoomMediumDesign, Ordered Set, Heap (Priority Queue)
912Sort an ArrayMediumArray, Divide and Conquer, Bucket Sort +5
973K Closest Points to OriginMediumGeometry, Array, Math +4
1054Distant BarcodesMediumGreedy, Array, Hash Table +3
1094Car PoolingMediumArray, Prefix Sum, Sorting +2
1268Search Suggestions SystemMediumTrie, Array, String +3
1338Reduce Array Size to The HalfMediumGreedy, Array, Hash Table +2
1353Maximum Number of Events That Can Be AttendedMediumGreedy, Array, Sorting +1
1405Longest Happy StringMediumGreedy, String, Heap (Priority Queue)
1424Diagonal Traverse IIMediumArray, Sorting, Heap (Priority Queue)
1438Longest Continuous Subarray With Absolute Diff Less Than or Equal to LimitMediumQueue, Array, Ordered Set +3
1488Avoid Flood in The CityMediumGreedy, Array, Hash Table +2
1514Path with Maximum ProbabilityMediumGraph, Array, Shortest Path +1
1631Path With Minimum EffortMediumDepth-First Search, Breadth-First Search, Union Find +4
1642Furthest Building You Can ReachMediumGreedy, Array, Heap (Priority Queue)
1648Sell Diminishing-Valued Colored BallsMediumGreedy, Array, Math +3
1686Stone Game VIMediumGreedy, Array, Math +3
1696Jump Game VIMediumQueue, Array, Dynamic Programming +2
1705Maximum Number of Eaten ApplesMediumGreedy, Array, Heap (Priority Queue)
1738Find Kth Largest XOR Coordinate ValueMediumBit Manipulation, Array, Divide and Conquer +5
1753Maximum Score From Removing StonesMediumGreedy, Math, Heap (Priority Queue)
1786Number of Restricted Paths From First to Last NodeMediumGraph, Topological Sort, Dynamic Programming +2
1792Maximum Average Pass RatioMediumGreedy, Array, Heap (Priority Queue)
1801Number of Orders in the BacklogMediumArray, Simulation, Heap (Priority Queue)
1834Single-Threaded CPUMediumArray, Sorting, Heap (Priority Queue)
1845Seat Reservation ManagerMediumDesign, Heap (Priority Queue)
1878Get Biggest Three Rhombus Sums in a GridMediumArray, Math, Matrix +3
1882Process Tasks Using ServersMediumArray, Heap (Priority Queue)
1942The Number of the Smallest Unoccupied ChairMediumArray, Hash Table, Heap (Priority Queue)
1962Remove Stones to Minimize the TotalMediumGreedy, Array, Heap (Priority Queue)
1985Find the Kth Largest Integer in the ArrayMediumArray, String, Divide and Conquer +3
2034Stock Price FluctuationMediumDesign, Hash Table, Data Stream +2
2054Two Best Non-Overlapping EventsMediumArray, Binary Search, Dynamic Programming +2
2146K Highest Ranked Items Within a Price RangeMediumBreadth-First Search, Array, Matrix +2
2182Construct String With Repeat LimitMediumGreedy, Hash Table, String +2
2208Minimum Operations to Halve Array SumMediumGreedy, Array, Heap (Priority Queue)
2233Maximum Product After K IncrementsMediumGreedy, Array, Heap (Priority Queue)
2285Maximum Total Importance of RoadsMediumGreedy, Graph, Sorting +1
2333Minimum Sum of Squared DifferenceMediumGreedy, Array, Binary Search +2
2336Smallest Number in Infinite SetMediumDesign, Hash Table, Ordered Set +1
2342Max Sum of a Pair With Equal Sum of DigitsMediumArray, Hash Table, Sorting +1
2343Query Kth Smallest Trimmed NumberMediumArray, String, Divide and Conquer +4
2349Design a Number Container SystemMediumDesign, Hash Table, Ordered Set +1
2353Design a Food Rating SystemMediumDesign, Array, Hash Table +3
2406Divide Intervals Into Minimum Number of GroupsMediumGreedy, Array, Two Pointers +3
2424Longest Uploaded PrefixMediumUnion Find, Design, Binary Indexed Tree +5
2456Most Popular Video CreatorMediumArray, Hash Table, String +2
2462Total Cost to Hire K WorkersMediumArray, Two Pointers, Simulation +1
2497Maximum Star Sum of a GraphMediumGreedy, Graph, Array +2
2512Reward Top K StudentsMediumArray, Hash Table, String +2
2530Maximal Score After Applying K OperationsMediumGreedy, Array, Heap (Priority Queue)
2542Maximum Subsequence ScoreMediumGreedy, Array, Sorting +1
2593Find Score of an Array After Marking All ElementsMediumArray, Hash Table, Sorting +2
2611Mice and CheeseMediumGreedy, Array, Sorting +1
2662Minimum Cost of a Path With Special RoadsMediumGraph, Array, Shortest Path +1
2679Sum in a MatrixMediumArray, Matrix, Sorting +2
2762Continuous SubarraysMediumQueue, Array, Ordered Set +3
2812Find the Safest Path in a GridMediumBreadth-First Search, Union Find, Array +3
2944Minimum Number of Coins for FruitsMediumQueue, Array, Dynamic Programming +2
253Meeting Rooms IIPremiumMediumGreedy, Array, Two Pointers +3
505The Maze IIPremiumMediumDepth-First Search, Breadth-First Search, Graph +4
1057Campus BikesPremiumMediumArray, Sorting, Heap (Priority Queue)
1102Path With Maximum Minimum ValuePremiumMediumDepth-First Search, Breadth-First Search, Union Find +4
1135Connecting Cities With Minimum CostPremiumMediumUnion Find, Graph, Minimum Spanning Tree +1
1167Minimum Cost to Connect SticksPremiumMediumGreedy, Array, Heap (Priority Queue)
1500Design a File Sharing SystemPremiumMediumDesign, Hash Table, Data Stream +2
1810Minimum Path Cost in a Hidden GridPremiumMediumDepth-First Search, Breadth-First Search, Graph +5
2015Average Height of Buildings in Each SegmentPremiumMediumGreedy, Array, Sorting +1
2093Minimum Cost to Reach City With DiscountsPremiumMediumGraph, Shortest Path, Heap (Priority Queue)
2473Minimum Cost to Buy ApplesPremiumMediumGraph, Array, Shortest Path +1
2599Make the Prefix Sum Non-negativePremiumMediumGreedy, Array, Heap (Priority Queue)
2737Find the Closest Marked NodePremiumMediumGraph, Array, Shortest Path +1

Hard (63)

#ProblemDifficultyTopics
23Merge k Sorted ListsHardLinked List, Divide and Conquer, Heap (Priority Queue) +1
218The Skyline ProblemHardBinary Indexed Tree, Segment Tree, Array +5
239Sliding Window MaximumHardQueue, Array, Sliding Window +2
295Find Median from Data StreamHardDesign, Two Pointers, Data Stream +2
407Trapping Rain Water IIHardBreadth-First Search, Array, Matrix +1
420Strong Password CheckerHardGreedy, String, Heap (Priority Queue)
480Sliding Window MedianHardArray, Hash Table, Sliding Window +1
502IPOHardGreedy, Array, Sorting +1
630Course Schedule IIIHardGreedy, Array, Sorting +1
632Smallest Range Covering Elements from K ListsHardGreedy, Array, Hash Table +3
675Cut Off Trees for Golf EventHardBreadth-First Search, Array, Matrix +1
778Swim in Rising WaterHardDepth-First Search, Breadth-First Search, Union Find +4
857Minimum Cost to Hire K WorkersHardGreedy, Array, Sorting +1
862Shortest Subarray with Sum at Least KHardQueue, Array, Binary Search +4
871Minimum Number of Refueling StopsHardGreedy, Array, Dynamic Programming +1
882Reachable Nodes In Subdivided GraphHardGraph, Shortest Path, Heap (Priority Queue)
1172Dinner Plate StacksHardStack, Design, Hash Table +1
1263Minimum Moves to Move a Box to Their Target LocationHardBreadth-First Search, Array, Matrix +1
1354Construct Target Array With Multiple SumsHardArray, Heap (Priority Queue)
1368Minimum Cost to Make at Least One Valid Path in a GridHardBreadth-First Search, Graph, Array +3
1383Maximum Performance of a TeamHardGreedy, Array, Sorting +1
1388Pizza With 3n SlicesHardGreedy, Array, Dynamic Programming +1
1425Constrained Subsequence SumHardQueue, Array, Dynamic Programming +3
1439Find the Kth Smallest Sum of a Matrix With Sorted RowsHardArray, Binary Search, Matrix +1
1499Max Value of EquationHardQueue, Array, Sliding Window +2
1606Find Servers That Handled Most Number of RequestsHardArray, Ordered Set, Simulation +1
1675Minimize Deviation in ArrayHardGreedy, Array, Ordered Set +1
1687Delivering Boxes from Storage to PortsHardSegment Tree, Queue, Array +4
1776Car Fleet IIHardStack, Array, Math +2
1825Finding MK AverageHardDesign, Queue, Data Stream +2
1851Minimum Interval to Include Each QueryHardArray, Binary Search, Sorting +2
1912Design Movie Rental SystemHardDesign, Array, Hash Table +2
2102Sequentially Ordinal Rank TrackerHardDesign, Data Stream, Ordered Set +1
2163Minimum Difference in Sums After Removal of ElementsHardArray, Dynamic Programming, Heap (Priority Queue)
2290Minimum Obstacle Removal to Reach CornerHardBreadth-First Search, Graph, Array +3
2344Minimum Deletions to Make Array DivisibleHardArray, Math, Number Theory +2
2386Find the K-Sum of an ArrayHardArray, Sorting, Heap (Priority Queue)
2398Maximum Number of Robots Within BudgetHardQueue, Array, Binary Search +4
2402Meeting Rooms IIIHardArray, Hash Table, Sorting +2
2454Next Greater Element IVHardStack, Array, Binary Search +3
2503Maximum Number of Points From Grid QueriesHardBreadth-First Search, Union Find, Array +4
2532Time to Cross a BridgeHardArray, Simulation, Heap (Priority Queue)
2551Put Marbles in BagsHardGreedy, Array, Sorting +1
2577Minimum Time to Visit a Cell In a GridHardBreadth-First Search, Graph, Array +3
2617Minimum Number of Visited Cells in a GridHardStack, Breadth-First Search, Union Find +5
2642Design Graph With Shortest Path CalculatorHardGraph, Design, Shortest Path +1
2699Modify Graph Edge WeightsHardGraph, Shortest Path, Heap (Priority Queue)
2813Maximum Elegance of a K-Length SubsequenceHardStack, Greedy, Array +3
2931Maximum Spending After Buying ItemsHardGreedy, Array, Matrix +2
2940Find Building Where Alice and Bob Can MeetHardStack, Binary Indexed Tree, Segment Tree +4
2959Number of Possible Sets of Closing BranchesHardBit Manipulation, Graph, Enumeration +2
2973Find Number of Coins to Place in Tree NodesHardTree, Depth-First Search, Dynamic Programming +2
272Closest Binary Search Tree Value IIPremiumHardStack, Tree, Depth-First Search +4
358Rearrange String k Distance ApartPremiumHardGreedy, Hash Table, String +3
499The Maze IIIPremiumHardDepth-First Search, Breadth-First Search, Graph +5
642Design Search Autocomplete SystemPremiumHardDepth-First Search, Design, Trie +4
683K Empty SlotsPremiumHardBinary Indexed Tree, Segment Tree, Queue +5
759Employee Free TimePremiumHardArray, Sorting, Line Sweep +1
1168Optimize Water Distribution in a VillagePremiumHardUnion Find, Graph, Minimum Spanning Tree +1
1183Maximum Number of OnesPremiumHardGreedy, Math, Sorting +1
1199Minimum Time to Build BlocksPremiumHardGreedy, Array, Math +1
2714Find Shortest Path with K HopsPremiumHardGraph, Shortest Path, Heap (Priority Queue)
2969Minimum Number of Coins for Fruits IIPremiumHardQueue, Array, Dynamic Programming +2

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.