LeetCode 42: How Much Rain Water Can an Elevation Map Hold?

Problem Requirement You are given n non-negative integers in height. Each integer is the height of a bar with width 1, and all bars are adjacent from left to right. After rain, taller bars on both sides may hold water above shorter bars. Return the total amount of water trapped by the entire elevation map. LeetCode expects this interface: class Solution: def trap(self, height: List[int]) -> int: ... Example 1 Input: height = [0,1,0,2,1,0,1,3,2,1,2,1] Output: 6 Example 2 Input: height = [4,2,0,3,2,5] Output: 9 Constraints n == len(height) 1 <= n <= 2 * 10^4 0 <= height[i] <= 10^5 Step 1: First Answer How Much Water One Position Holds Do not calculate the whole elevation map yet. Focus on one position: ...

January 24, 2026 · 14 min · map[name:Jeanphilo]