Trapping Rain Water

Hard
Arrays Two Pointers Stack DP
Given n bars, compute how much water can be trapped. Input: space-separated heights Output: total water units

Constraints

See problem-specific constraints. Optimized solution required.

Examples

Example 1:
Input: 0 1 0 2 1 0 1 3 2 1 2 1
Output:
Example 2:
Input: 0 1 0 2 1 0 1 3 2 1 2 1
Output:

Hints

Consider a two pointers approach.
Think about time complexity.
▲ Console

Install Talent Arabia

Get instant access to jobs and career tools on your device.