<p>Given <code>n</code> non-negative integers representing an elevation map where the width of each bar is <code>1</code>, compute how much water it can trap after raining.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/rainwatertrap.png" style="width: 412px; height: 161px;" />
<pre>
<strong>Input:</strong> height = [0,1,0,2,1,0,1,3,2,1,2,1]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The above elevation map (black section) is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> height = [4,2,0,3,2,5]
<strong>Output:</strong> 9
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == height.length</code></li>
	<li><code>1 &lt;= n &lt;= 2 * 10<sup>4</sup></code></li>
	<li><code>0 &lt;= height[i] &lt;= 10<sup>5</sup></code></li>
</ul>

class Solution {
public:
    int brute_force(vector<int> height) {
        int n = height.size();
        int count = 0;

        for(int i = 1; i <= n-2; i++) {
            int left = 0;
            int right = 0;

            // left tallest building
            for(int l = i-1; l >= 0; l--)
                left = max(left, height[l]);
            
            // right tallest building
            for(int r = i+1; r < n; r++)
                right = max(right, height[r]);

            count += max(0, min(left,right)-height[i]);
        }

        return count;
    }

    int precompute(vector<int> height) {
        int n = height.size();
        int count = 0;

        vector<int> left_max(n, 0);
        vector<int> right_max(n, 0);

        left_max[0] = height[0];
        right_max[n-1] = height[n-1];

        for(int i = 1; i < n; i++) {
            left_max[i] = max(left_max[i-1], height[i]);
        }

        for(int i = n-2; i >= 0; i--) {
            right_max[i] = max(right_max[i+1], height[i]);
        }

        for(int i = 1; i <= n-2; i++) {
            count += max(0, min(left_max[i-1],right_max[i+1])-height[i]);
        }

        return count;
    }

    int optimal(vector<int> height) {
        int n = height.size();
        int count = 0;

        int l = 0;      // l holds lmax
        int r = n-1;    // r holds rmax

        int lmax = height[l];
        int rmax = height[r];

        while(l < r) {
            // if l is the limiting height
            if(height[l] < height[r]) {
                l++;
                count += max(0, min(lmax,rmax)-height[l]);
                lmax = max(height[l], lmax);
            }
            // else r is the limiting height
            else {
                r--;
                count += max(0, min(lmax,rmax)-height[r]);
                rmax = max(height[r], rmax);
            }
        }

        return count;
    }

    int trap(vector<int>& height) {
        // brute_force ----> stand at index => find the tallest building in left and right
        // max(0, min(left, right)-height(i))
        // return brute_force(height);

        // precompute - left and right max - so that this can be obtained in O(1)
        // return precompute(height);

        // just using 2 variables to keep track of lmax and rmax -> O(1) space
        return optimal(height);
    }
};
25400