#K49242. Trapping Rain Water
Trapping Rain Water
Trapping Rain Water
Given an elevation map represented by a list of non-negative integers, your task is to compute the total amount of water that can be trapped after it rains. The water trapped over a bar at position i is given by the formula:
$$W_i = \min(L_i, R_i) - h_i$$
where:
- $L_i$ is the maximum height to the left of index i,
- $R_i$ is the maximum height to the right of index i, and
- $h_i$ is the height at position i.
The total water trapped is the sum of water over each index. Implement a solution that reads the elevation map from standard input and outputs the total water trapped.
inputFormat
A single line of space-separated non-negative integers representing the elevation map. An empty input should be considered as an empty list.
outputFormat
An integer representing the total amount of trapped water.## sample
3 0 2 0 4
7