#K47577. Trapping Rain Water in Buildings
Trapping Rain Water in Buildings
Trapping Rain Water in Buildings
Given an array of non-negative integers where each integer represents the height of a building, your task is to calculate the total amount of water that can be trapped between these buildings after it rains.
For each building at index \(i\), the water that can be trapped on top of it is determined by the formula:
$$w_i = \max\Big(\min\big(L_i, R_i\big) - \text{buildings}[i],\; 0\Big)$$
where
- \(L_i = \max_{0\le j\le i}\ \text{buildings}[j]\) is the maximum height to the left of \(i\), and
- \(R_i = \max_{i\le j<n}\ \text{buildings}[j]\) is the maximum height to the right of \(i\).
The final answer is the sum of \(w_i\) for all indices \(i\).
inputFormat
The input consists of a single line containing space-separated integers, each representing the height of a building. If the input is empty, consider it as an empty list.
outputFormat
Output a single integer representing the total amount of water trapped between the buildings.
## sample0 1 2 1 0 1
1