题解 | #盛水最多的容器#
盛水最多的容器
https://www.nowcoder.com/practice/3d8d6a8e516e4633a2244d2934e5aa47
跳过指针移动过程中经过的比该循环初始时的值还要小的位置,这些位置不可能是最大容量,因为底面长度和边长都在缩短。
static const auto io_sync_off = []() {
std::ios::sync_with_stdio(false); //关闭输入输出
std::cin.tie(nullptr); //取消两个stream绑定
std::cout.tie(
nullptr); //取消cin 和 cout之间的绑定,加快执行效率
return nullptr;
}
();
class Solution {
public:
int maxArea(vector<int>& height) {
if (height.size() < 2) {
return 0;
}
int res = 0, left = 0, right = height.size() - 1;
while (left < right) {
if (height[left] < height[right]) {
res = max(res, height[left] * (right - left));
while (height[left + 1] <= height[left]) {
left++;
}
left++;
} else {
res = max(res, height[right] * (right - left));
while (height[right - 1] <= height[right]) {
right--;
}
right--;
}
}
return res;
}
};
