Back to problems Sign in to unlock
Leetcode
Medium
Arrays
Stacks
Two Pointers
Amazon
Trapping Rain Water
Compute how much rainwater can be trapped between vertical bars after rainfall.
Acceptance 94%
Problem Statement
Trapping Rain Water
You are given an array of non-negative integers where each value represents the height of a vertical bar of width 1. The bars are placed next to each other in order. After it rains, water can be trapped between the bars.
Return the total amount of water that can be stored above all bars.
Water above a bar is limited by the tallest bar to its left and the tallest bar to its right. The amount trapped at an index depends on the shorter of those two boundaries.
Input Format
- An integer array
heightof lengthn. - Each
height[i]is a non-negative integer representing the height of the i-th bar.
Output Format
- Return a single integer: the total units of water trapped.
Constraints
- Heights are non-negative integers.
- The answer fits in a 32-bit signed integer.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.
Guided hints
Editorial and discussion links
Concept map and variants
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.