leetcode

11.盛最多水的容器

力扣链接

双指针

class Solution {
public:
    int maxArea(vector<int>& height) {
    	int left = 0, right = height.size() -1, res = 0;
    	while (left < right){
    		res = std::max(res, (right-left)*std::min(height[left], height[right]));
            // 向内移动短板,面积可能增大;向内移动长板,面积一定减小
    		height[left] < height[right] ? left++ : right--;
            // res = height[left] < height[right] ? std::max(res, (right -left)*height[left++]) : std::max(res, (right -left)*height[right--]);
    	}
    	return res;
    }
};

暴力解法超时

class Solution {
public:
    int maxArea(std::vector<int>& height) {
        int res=0;
        for (int i=0; i<height.size();i++)
            for(int j=i+1;j<height.size();j++)
                res = std::max(res, std::min(height[i], height[j])*(j-i));
        return res;
    }
};