Tags

Monotonic Stack

Monotonic Stack

1 lesson
7 problems
1 question bank
2 community items

monotonic-stack

Algorithms

1 lesson

Advanced Greedy & Data Structures

Advanced

65 min

2 prereqs

"For each element in the array, find the next greater element" is a brute-force `O(n^2)` problem until you notice that a stack maintained in decreasing order lets you answer every query as a side effect of a single left-to-right scan. The whole algorithm runs in `O(n)`, and the same monotonic-stack pattern solves trapping rain water, largest rectangle in a histogram, stock spans, and a long tail of related problems with the same template. **Advanced Greedy & Data Structures** is where greedy thinking meets specialized scaffolding. You will implement the monotonic stack pattern for next-greater and next-smaller variants, the monotonic deque for sliding-window maximum and minimum (also a key DP optimization), and sweep-line algorithms for interval and event problems (meeting rooms, interval merge, rectangle area union). The lesson closes with advanced greedy problems that need a heap or priority queue: full Huffman encoding, task scheduling with cooldown, the gas station problem, and activity selection with deadlines and profits. In **Greedy (Intro)**, you saw simple greedy strategies that needed only sorting. **Heaps & Priority Queue** taught you `O(log n)` access to the minimum or maximum, which is exactly what these advanced greedy patterns rely on for their efficiency. Next, **Branch and Bound** extends backtracking with the same kind of bounding-function pruning, applied to optimization search trees.

Not Started

0%

Algorithms
Greedy
Monotonic Stack
Monotonic Queue
Sweep Line
Next Greater Element
Largest Rectangle in Histogram
Advanced
Premium

Practice Problems

7 problems

Next Greater Element I

Free
Not Started
Easy

Given two arrays where nums1 is a subset of nums2, find the next greater element in nums2 for each element in nums1.

Stack
Monotonic Stack
Hash Map / Dictionary
Beginner

896

27

Largest Rectangle in Histogram

Not Started
Hard

Given an array of bar heights representing a histogram, find the area of the largest rectangle that can be formed within the histogram.

Stack
Monotonic Stack
Largest Rectangle in Histogram
Advanced

888

23

Maximal Rectangle

Not Started
Hard

Given a 2D binary matrix filled with '0's and '1's, find the area of the largest rectangle containing only '1's.

Stack
Monotonic Stack
Dynamic Programming
Largest Rectangle in Histogram
Advanced

653

17

Trapping Rain Water

Not Started
Hard

Given an elevation map, compute how much water it can trap after raining.

Two Pointers
Arrays
Trapping Rain Water
Monotonic Stack
Advanced

335

2

Car Fleet

Not Started
Medium

Given positions and speeds of cars heading toward a target, determine how many car fleets will arrive at the destination.

Stack
Monotonic Stack
Sorting
Intermediate

304

4

Daily Temperatures

Not Started
Medium

Given an array of daily temperatures, return how many days you have to wait for a warmer temperature for each day.

Stack
Monotonic Stack
Next Greater Element
Intermediate

969

14

Sum of Subarray Minimums

Not Started
Medium

Given an array of integers, find the sum of the minimum value of every contiguous subarray, modulo 10^9 + 7.

Stack
Monotonic Stack
Dynamic Programming
Intermediate

651

4