题目: https://leetcode.com/problems/container-with-most-water/
概述: 给定一些木板的高度,求哪两块木板底乘高为最大值。
思路: i 和 j 从两边开始走,如果他们不能提高更高的高度,就进行忽略,记住是小于等于,否则就不往前走了。
class Solution {
public:
int maxArea(vector<int>& height) {
int water = 0;
int i = 0, j = height.size() - 1;
while(i < j)
{
int h = min(height[i], height[j]);
water = max(water, h * (j - i));
while(height[i] <= h && i < j) i++;
while(height[j] <= h && i < j) j--;
}
return water;
}
};