Monotonic-Stack
Stack and Monotonic Stack
·6 mins
Algorithm
Stack
Monotonic-Stack
Greedy
Simulation
Parsing nested structures, simulations that interact with the previous element, monotonic stacks, greedy deletion for the smallest string, and a lazy-deletion Max Stack
LeetCode 4045: Count Robot Groups
·3 mins
LeetCode
Medium
Stack
Monotonic-Stack
Greedy
LeetCode 4045 note: merge robots already close at t = 0, then go right to left like car fleets, where anything faster than the fleet ahead eventually joins it
LeetCode 1944: Number of Visible People in a Queue
·2 mins
LeetCode
Hard
Stack
Monotonic-Stack
LeetCode 1944 note: a decreasing monotonic stack from right to left; everyone popped is visible, and so is the person left on top
LeetCode 2355: Maximum Number of Books You Can Take
·2 mins
LeetCode
Hard
Dynamic-Programming
Monotonic-Stack
Stack
Array
LeetCode 2355 note: ending at i, each shelf to the left takes one fewer book until a bottleneck shelf j with too few books; a monotonic stack finds j, and dp[i] = dp[j] + an arithmetic series
LeetCode 316: Remove Duplicate Letters
·2 mins
LeetCode
Medium
Stack
Monotonic-Stack
Greedy
String
LeetCode 316 note: a monotonic stack with last positions; pop a larger top that appears again later, and skip letters already on the stack
LeetCode 1526: Minimum Number of Increments on Subarrays to Form a Target Array
·1 min
LeetCode
Hard
Dynamic-Programming
Greedy
Monotonic-Stack
Array
LeetCode 1526 note: scanning left to right, every rise above the previous height needs that many new operations; drops reuse earlier operations
LeetCode 581: Shortest Unsorted Continuous Subarray
·2 mins
LeetCode
Medium
Two-Pointers
Greedy
Sorting
Array
Monotonic-Stack
LeetCode 581 note: scanning left to right with a running max, the last index below it is the right boundary; scanning right to left with a running min, the last index above it is the left boundary
LeetCode 3555: Smallest Subarray to Sort in Every Sliding Window
·2 mins
LeetCode
Medium
Two-Pointers
Greedy
Sorting
Array
Monotonic-Stack
LeetCode 3555 note: every length-k window is one instance of LeetCode 581; with n only 1000, two O(k) scans per window are enough
LeetCode 1793: Maximum Score of a Good Subarray
·3 mins
LeetCode
Hard
Two-Pointers
Greedy
Binary-Search
Array
Monotonic-Stack
LeetCode 1793 note: expand from k toward the larger neighbor each step so the minimum stays as large as possible for each length; at a boundary, only the other side can grow
LeetCode 1574: Shortest Subarray to be Removed to Make Array Sorted
·2 mins
LeetCode
Medium
Two-Pointers
Binary-Search
Array
Monotonic-Stack
LeetCode 1574 note: what remains is a non-decreasing prefix plus a non-decreasing suffix; find the longest of each, then join them with two pointers
LeetCode 3816: Lexicographically Smallest String After Deleting Duplicate Characters
·3 mins
LeetCode
Hard
Stack
Monotonic-Stack
Greedy
String
LeetCode 3816 note: a monotonic stack that deletes a larger top while another copy exists, then trims duplicate letters off the end since a prefix is always smaller
LeetCode 901: Online Stock Span
·2 mins
LeetCode
Daily
Medium
Stack
Monotonic-Stack
Data-Structures
Design
Design-Patterns
Stock
Solving the Online Stock Span problem using monotonic stack approach