题解 | #盛水最多的容器#
盛水最多的容器
https://www.nowcoder.com/practice/3d8d6a8e516e4633a2244d2934e5aa47
双指针:从两头向中间,指针移动过程中跳过比上一步经过的值小的位置,因为只需要求最大值即可。
#include <algorithm>
#include <vector>
class Solution {
public:
int Area(vector<int>& height, int l, int r) {
return (r - l) * min(height[l], height[r]);
}
int maxArea(vector<int>& height) {
if (height.size() < 2) {
return 0;
}
int res = 0, left = 0;
for (int i = 0; i < height.size(); i++) {
if (height[i] > left) {
int right = 0;
for (int j = height.size() - 1; j > i; j--) {
if (height[j] > right) {
int temp = Area(height, i, j);
res = res < temp ? temp : res;
right = height[j];
}
}
left = height[i];
}
}
return res;
}
};