01. Command Hub POTD 02. IDE Workbench SPLIT IDE 03. Visualizer Pro VISUALGO 2.0 04. Skill Matrix ROADMAP 05. 1v1 Duels WARZONE
TARGET: TIME O(N) SPACE O(1)
CLEARANCE: LEVEL-4
HARD ACCEPTANCE: 61.4% SECTOR 01: TWO POINTERS BOUNTY: +60 0xC

42. Trapping Rain Water

Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining.

Your solution must operate in strict O(N) time complexity and optimal O(1) auxiliary space complexity using two opposing spatial memory pointers.

Example 1:
Input: height = [0,1,0,2,1,0,1,3,2,1,2,1]
Output: 6
Explanation: The elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water are trapped in the depressions.
Example 2:
Input: height = [4,2,0,3,2,5]
Output: 9
CONSTRAINTS & MEMORY BOUNDS:
  • n == height.length
  • 1 <= n <= 2 * 10^4
  • 0 <= height[i] <= 10^5
  • Peak auxiliary stack/heap allocation allowed: ≤ 16 MB
AUTO-SYNC READY
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
SPATIAL MEMORY ENGINE Pointers: L=3, R=8 | Trapped: 6 units