Keep track of the already safe `level`

and the total `water`

so far. In each step, process and discard the `lower`

one of the leftmost or rightmost elevation.

**C**

Changing the given parameters to discard the lower border. I'm quite fond of this one.

```
int trap(int* height, int n) {
int level = 0, water = 0;
while (n--) {
int lower = *height < height[n] ? *height++ : height[n];
if (lower > level) level = lower;
water += level - lower;
}
return water;
}
```

Slight variation with two pointers (left and right).

```
int trap(int* height, int n) {
int *L = height, *R = L+n-1, level = 0, water = 0;
while (L < R) {
int lower = *L < *R ? *L++ : *R--;
if (lower > level) level = lower;
water += level - lower;
}
return water;
}
```

**C++**

With left and right index.

```
int trap(vector<int>& height) {
int l = 0, r = height.size()-1, level = 0, water = 0;
while (l < r) {
int lower = height[height[l] < height[r] ? l++ : r--];
level = max(level, lower);
water += level - lower;
}
return water;
}
```

With left and right iterator.

```
int trap(vector<int>& height) {
auto l = height.begin(), r = height.end() - 1;
int level = 0, water = 0;
while (l != r + 1) {
int lower = *l < *r ? *l++ : *r--;
level = max(level, lower);
water += level - lower;
}
return water;
}
```