THN Interview Prep

042. Trapping Rain Water

At a Glance

  • Topic: Array
  • Pattern: Analyze Pattern
  • Difficulty: Hard
  • LeetCode: 042

Problem Statement

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.

Example 1:

Input: height = [0,1,0,2,1,0,1,3,2,1,2,1] Output: 6 Explanation: The above elevation map (black section) is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped.

Example 2:

Input: height = [4,2,0,3,2,5] Output: 9

Constraints:

n == height.length
1 <= n <= 2 * 104
0 <= height[i] <= 105

Approach & Solution Steps

  • Naive scanning is O(n^2). Prefix/suffix max arrays are O(n) space. Two pointers are O(n) time and O(1) space.

Optimal JS Solution

function trap(height) {
	let left = 0;
	let right = height.length - 1;
	let leftMax = 0;
	let rightMax = 0;
	let water = 0;

	while (left < right) {
		if (height[left] < height[right]) {
			if (height[left] >= leftMax) leftMax = height[left];
			else water += leftMax - height[left];
			left++;
		} else {
			if (height[right] >= rightMax) rightMax = height[right];
			else water += rightMax - height[right];
			right--;
		}
	}

	return water;
}

Edge Cases & Pitfalls

  • Always consider empty or null inputs.
  • Watch out for off-by-one index errors.

Mark this page when you finish learning it.

Last updated on

Spotted something unclear or wrong on this page?

On this page