Grokking Data Structures & Algorithms for Coding Interviews
0% completed
Solution: Trapping Rain Water
Problem Statement
You are given an array height where height[i] is the height of a bar of width 1. Return how many units of rainwater are trapped between the bars.
Examples
Example 1
- Input: height =
[0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1] - Expected Output:
6
Example 2
- Input: height =
[4, 2, 0, 3, 2, 5] - Expected Output:
9
Example 3
- Input: height =
[3, 2, 1] - Expected Output:
0
The one fact the whole problem rests on
Look at a single position i and ask how deep the water is there
.....
.....
.....
Like the course? Get enrolled and start learning!
Reading Progress
0%