Problem
You get an array of non-negative integers height. Each item is the height of a bar with a width of 1. The bars stand next to each other in a row.
Rain falls on the bars. Water stays in a low area when a taller bar stands on its left side and on its right side. Water that reaches either end of the row flows away.
Return the total amount of water that stays on the bars. One unit of water fills a square with a width of 1 and a height of 1.
Example 1
- Input:
- height = [3,0,2,0,4]
- Output:
- 7
- Explanation:
- Water fills indexes 1, 2 and 3 up to the height 3. The amounts are 3, 1 and 3, so the total is 7.
Example 2
- Input:
- height = [1,2,3]
- Output:
- 0
- Explanation:
- The heights only go up, so no water stays.
Example 3
- Input:
- height = [5,1,1,6,0,2]
- Output:
- 10
Constraints
- n == height.length
- 1 <= n <= 2 * 10^4
- 0 <= height[i] <= 10^5
SysRound wrote this statement. A reference solution checks every example.
Related questions
Continue with questions that share topics or the same question type.
- 132 Pattern
Coding · Array · Binary Search · Monotonic Stack
- Beautiful Towers II
Coding · Array · Monotonic Stack · Stack
- Binary Searchable Numbers in an Unsorted Array
Coding · Array · Binary Search · Monotonic Stack
- Buildings With an Ocean View
Coding · Array · Monotonic Stack · Stack
Sources · 2 references
These records explain where the question appears. A company tag or candidate report does not guarantee a future interview question.
Hello Interview · 6 records
Amazon
Leetcode 42. Trapping Rain Water
Reported Oct 1, 2026 · 6 source reports
JUNIOR, MID_LEVEL, SENIOR
Source snapshot: Nov 12, 2026
View sourceBloomberg
Leetcode 42. Trapping Rain Water
Reported Aug 1, 2026 · 4 source reports
MID_LEVEL, SENIOR
Source snapshot: Nov 12, 2026
View sourceGoldman Sachs
Leetcode 42. Trapping Rain Water
Reported Jul 31, 2026 · 4 source reports
MID_LEVEL, SENIOR
Source snapshot: Nov 12, 2026
View sourceMarvin
Leetcode 42. Trapping Rain Water
Reported Dec 6, 2025 · 1 source report
MID_LEVEL
Source snapshot: Nov 12, 2026