Queue
Queue and Design Problems
·3 mins
Algorithm
Queue
Design
Iterator
Lazy-Deletion
Circular queues, zigzag iterators, FIFO matching with cancellation, and the data structure combinations that design problems keep using
LeetCode 622: Design Circular Queue
·2 mins
LeetCode
Medium
Queue
Design
Array
LeetCode 622 note: a fixed array plus head and count, every position taken modulo the capacity, and count deciding empty versus full
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 281: Zigzag Iterator
·2 mins
LeetCode
Medium
Queue
Design
Iterator
LeetCode 281 note: a queue of unfinished sources, take one value in turn and put the source back if anything remains; it generalizes to k sources
LeetCode 3835: Count Subarrays With Cost Less Than or Equal to K
·2 mins
LeetCode
Medium
Sliding-Window
Monotonic-Queue
Queue
Array
LeetCode 3835 note: cost = (max - min) × length only grows as the window grows, so a sliding window counts subarrays; two monotonic deques keep the window’s max and min
LeetCode 3829: Design Ride Sharing System
·2 mins
LeetCode
Medium
Queue
Design
Hash-Map
Lazy-Deletion
LeetCode 3829 note: one queue each for riders and drivers; cancellations only update a set, and canceled riders at the front are dropped before matching
LeetCode 1696: Jump Game VI
·2 mins
LeetCode
Medium
Dynamic-Programming
Monotonic-Queue
Queue
Heap
LeetCode 1696 note: dp[i] = nums[i] + max of the previous k dp values, with that max kept by a monotonic deque in O(n)
LeetCode 3508: Implement Router
·3 mins
LeetCode
Medium
Design
Binary-Search
Queue
Hash-Map
LeetCode 3508 note: a queue for FIFO, a set for duplicates, and an increasing time list per destination queried with bisect
LeetCode 387: First Unique Character in a String
·1 min
LeetCode
Easy
Hash-Table
String
Queue
Counting
LeetCode 387 note: count first, then scan left to right for the first character with count 1
LeetCode 232: Implement Queue using Stacks
·1 min
LeetCode
Easy
Stack
Design
Queue
LeetCode 232 note: two stacks, where in receives pushes and out is refilled from in only when empty, amortized O(1)
LeetCode 225: Implement Stack using Queues
·2 mins
LeetCode
Easy
Stack
Design
Queue
LeetCode 225 note: one queue suffices: after a push, rotate all earlier elements to the back so the new element sits at the front