two-dimensional elevation

This Question Was Asked By Facebook

You are given an array of non-negative integers that represents a two-dimensional elevation map where each element is a unit-width wall and the integer is the height. Suppose it will rain and all spots between two walls get filled up. Compute how many units of water remain trapped on the map in O(N) time and O(1) space.

Write a function solve that have the following parameter(s):
1.) arr = an array of integer

Example
Input:
[3, 0, 1, 3, 0, 5] Output:
The function will return,
8

Explanation
An array of integer [3, 0, 1, 3, 0, 5] given, we can hold 3 units in the first index, 2 in the second, and 3 in the fourth index. We cannot hold 5 since it would run off to the left. So, we can trap 8 units of water.

Constraints
• The length of an array should be greater than 0 and less than 800.
• Each element of an array should be greater than 0 and less than 1000.