0%

LeetCode 11. Container With Most Water

题目

原题在此

解析

从最宽的开始, 如果比它窄的箱子想装更多水, 就一定要比它高.

代码

c++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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, (j - i) * h);
while (height[i] <= h && i < j) i++;
while (height[j] <= h && i < j) j--;
}
return water;
}
};