LeetCopilot Logo
LeetCopilot
LeetCode Pattern/Monotonic Stack & Queue/Trapping Rain Water: Edge Cases and Two Solution Approaches

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:

  1. Monotonic Stack: O(n) time, O(n) space
  2. Two Pointers: O(n) time, O(1) space ✨

5 edge cases:

  1. Empty or single bar
  2. No water trapped (increasing/decreasing)
  3. All same height
  4. Valley at start/end
  5. 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 water

Approach 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 water

Edge Cases

Edge Case 1: Empty or Single Bar

python
assert trap([]) == 0
assert trap([5]) == 0

Edge Case 2: Strictly Increasing

python
assert trap([1, 2, 3, 4, 5]) == 0
# No water trapped

Edge Case 3: Strictly Decreasing

python
assert trap([5, 4, 3, 2, 1]) == 0
# No water trapped

Edge Case 4: All Same Height

python
assert trap([3, 3, 3, 3]) == 0
# No valleys

Edge 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 end

When 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

Related Tutorials