Containing With Most Water

code

class Solution {
public:
    /**
     * @param heights: a vector of integers
     * @return: an integer
     */
    int maxArea(vector<int> &heights) {
        // write your code here
        int sum = 0;
        if(heights.size() == 0)
        {
            return sum;
        }

        int l = 0;
        int r = heights.size()-1;

        while(l != r)
        {
            sum = max(sum, min(heights[l], heights[r]) * (r-l));
            if(heights[l] < heights[r])
            {
                l++;
            }
            else
            {
                r--;
            }
        }

        return sum;
    }
};

results matching ""

    No results matching ""