Heap
LeetCode 23: Merge k Sorted Lists
·4 mins
LeetCode
Hard
Linked-List
Heap
Merge-Sort
Divide-and-Conquer
LeetCode 23 note: a heap that holds only k heads, and divide-and-conquer pairwise merging
Heap and Priority Queue
·6 mins
Algorithm
Heap
Priority-Queue
Greedy
Dijkstra
heapq basics, Top-K, lazy deletion, two heaps, k-way merge, sweep line, and Dijkstra
LeetCode 253: Meeting Rooms II
·2 mins
LeetCode
Medium
Heap
Sorting
Intervals
Sweep-Line
LeetCode 253 note: a heap of end times for running meetings; half-open intervals need <=
LeetCode 716: Max Stack
·3 mins
LeetCode
Hard
Stack
Heap
Design
Lazy-Deletion
LeetCode 716 note: store unique ids on both the stack and the heap, record deletions by id, and discard deleted entries lazily; tracking values cannot tell duplicates apart
LeetCode 2973: Find Number of Coins to Place in Tree Nodes
·3 mins
LeetCode
Hard
Dynamic-Programming
Tree-Dp
Depth-First-Search
Sorting
Heap
Tree
LeetCode 2973 note: the max product of three is either the three largest or the two smallest times the largest, so each subtree returns only its 2 smallest and 3 largest values, merged in post-order
LeetCode 4046: Minimum Cost Path With At Most K Turns
·3 mins
LeetCode
Hard
Graph
Shortest-Path
Heap
Matrix
LeetCode 4046 note: Dijkstra with the last direction and the turns used in the state; for the same cell and direction, an earlier pop is cheaper, and if it also used fewer turns it dominates later states
LeetCode 1425: Constrained Subsequence Sum
·2 mins
LeetCode
Hard
Sliding-Window
Dynamic-Programming
Monotonic-Queue
Heap
Queue
Array
LeetCode 1425 note: dp[i] = nums[i] + max(0, max of the previous k dp values); that max is a sliding window maximum, available in O(1) from a monotonic deque
LeetCode 1354: Construct Target Array With Multiple Sums
·3 mins
LeetCode
Hard
Heap
Math
Greedy
LeetCode 1354 note: work backwards, since the maximum was set last; jump with a modulo when the gap is large
LeetCode 499: The Maze III
·3 mins
LeetCode
Hard
Graph
Dijkstra
Shortest-Path
Heap
Matrix
LeetCode 499 note: Dijkstra prioritized by (distance, path string), and the ball stops when it rolls over the hole
LeetCode 2163: Minimum Difference in Sums After Removal of Elements
·2 mins
LeetCode
Hard
Dynamic-Programming
Heap
Array
LeetCode 2163 note: try every boundary k between the two parts; the left takes the n smallest of nums[:k] (a max-heap) and the right the n largest of nums[k:] (a min-heap), one prefix and one suffix pass
LeetCode 2102: Sequentially Ordinal Rank Tracker
·3 mins
LeetCode
Hard
Heap
Design
Two-Heaps
LeetCode 2102 note: two heaps for “top k” and “the rest”, with a custom lt for reversed string order
LeetCode 1878: Get Biggest Three Rhombus Sums in a Grid
·3 mins
LeetCode
Medium
Matrix
Enumeration
Heap
LeetCode 1878 note: enumerate every top vertex and size, walk the four edges, and keep the three largest distinct sums