Two Pointers pattern
Trapping Rain Water
Given an elevation map of bar heights, compute how many units of water can be trapped between the bars after rain. Watch it run step by step, then write your own pseudocode and get it checked by AI.
- LeetCode 42
- Hard
- 3 approaches
Sign in with Google or GitHub and you land straight on this page. DSA comes with the all labs plan and every ChaiCode batch.
Two PointersLeetCode 42
Hard
From the first idea to the best one
- 01 Brute force · scan both sides per column O(n²)O(1) space
- 02 Better · precompute maxL[] and maxR[] arrays O(n)O(n) space
- 03 Optimized · two pointers, O(1) space O(n)O(1) space
What you will learn
- How to read Trapping Rain Water and spot the two pointers pattern in it
- Brute force · scan both sides per column: O(n²) time, O(1) space
- Better · precompute maxL[] and maxR[] arrays: O(n) time, O(n) space
- Optimized · two pointers, O(1) space: O(n) time, O(1) space
- The pseudocode for each approach, its line lit as the animation runs
- Practice: write your own pseudocode and get it checked
More Two Pointers
See Trapping Rain Water run, step by step
Sign in and this page opens on the animation, with all 12 Two Pointers problems beside it.
DSA comes with the all labs plan and every ChaiCode batch.