This is basically a variant of Problem 11: Container With Most Water


  • 0
    A

    Same logic, same concept.

    class Solution {
    public:
        int trap(vector<int>& height) {
            int b = 0, e = height.size()-1, sum = 0, p, i;
            while (b+1 < e) {
                p = height[b] < height[e] ? (i = 1, height[b]) : (i = 0, height[e]);
                while (i ? height[++b] < p : height[--e] < p) {
                        sum += i ? p - height[b] : p - height[e];
                }
            }
            return sum;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.