Stacks & Queues
Stacks (LIFO) and Queues (FIFO) are fundamental linear data structures. In advanced problems, they are not just used for storage but as mechanisms to enforce order and process elements in specific sequences.
A stack where elements are always in a sorted order (increasing or decreasing). It is highly effective for problems involving finding the next/previous greater/smaller element.
Example Problems:
Daily TemperaturesNext Greater Element I/IILargest Rectangle in Histogram
Solution Spotlight: Daily Temperatures
This solution uses a monotonic (decreasing) stack to store the indices of days. When a warmer day is found, indices are popped from the stack, and the waiting days are calculated. This ensures each index is processed in linear time.
DailyTemperatures.java
Loading code syntax...