LeetCode Pattern/Monotonic Stack & Queue/Trapping Rain Water: Edge Cases and Two Solution Approaches
LeetCopilot Team
Dec 22, 2025
4 min read
Monotonic StackTwo PointersTrapping Rain WaterEdge Cases
Master trapping rain water edge cases with both monotonic stack and two pointers approaches. Learn when to use each method and handle all corner cases correctly.
Trapping Rain Water has two optimal solutions and 5 critical edge cases that cause most failures.
This guide shows you both approaches and how to handle every edge case.
TL;DR
Two approaches:
- Monotonic Stack: O(n) time, O(n) space
- Two Pointers: O(n) time, O(1) space ✨
5 edge cases:
- Empty or single bar
- No water trapped (increasing/decreasing)
- All same height
- Valley at start/end
- Multiple valleys
Approach 1: Monotonic Stack
python
def trap_stack(height):
"""
Monotonic increasing stack approach.
Time: O(n)
Space: O(n)
"""
if not height:
return 0
water = 0
stack = []
for i in range(len(height)):
while stack and height[i] > height[stack[-1]]:
bottom = stack.pop()
if not stack:
break
left = stack[-1]
width = i - left - 1
bounded_height = min(height[left], height[i]) - height[bottom]
water += width * bounded_height
stack.append(i)
return waterApproach 2: Two Pointers (Better Space)
python
def trap_two_pointers(height):
"""
Two pointers approach.
Time: O(n)
Space: O(1) ← Better!
"""
if not height:
return 0
left, right = 0, len(height) - 1
left_max, right_max = height[left], height[right]
water = 0
while left < right:
if height[left] < height[right]:
if height[left] >= left_max:
left_max = height[left]
else:
water += left_max - height[left]
left += 1
else:
if height[right] >= right_max:
right_max = height[right]
else:
water += right_max - height[right]
right -= 1
return waterEdge Cases
Edge Case 1: Empty or Single Bar
python
assert trap([]) == 0
assert trap([5]) == 0Edge Case 2: Strictly Increasing
python
assert trap([1, 2, 3, 4, 5]) == 0
# No water trappedEdge Case 3: Strictly Decreasing
python
assert trap([5, 4, 3, 2, 1]) == 0
# No water trappedEdge Case 4: All Same Height
python
assert trap([3, 3, 3, 3]) == 0
# No valleysEdge Case 5: Valley at Boundaries
python
assert trap([0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]) == 6
# Handles valleys at start and endWhen to Use Each Approach
Use Two Pointers when:
- ✅ Space is constrained (O(1) vs O(n))
- ✅ Simpler to understand
- ✅ Interview preference (shows optimization)
Use Monotonic Stack when:
- ✅ Already using stack for other parts
- ✅ More intuitive for some developers
- ✅ Easier to visualize valleys
Conclusion
Two approaches, both O(n):
- Stack: O(n) space
- Two Pointers: O(1) space ✨
Edge cases:
- Empty, single, increasing, decreasing, same height
Recommendation: Learn both, prefer two pointers in interviews.
Next steps:
Want to Practice LeetCode Smarter?
LeetCopilot is a free browser extension that enhances your LeetCode practice with AI-powered hints, personalized study notes, and realistic mock interviews — all designed to accelerate your coding interview preparation.
Also compatible with Edge, Brave, and Opera
