http://oj.leetcode.com/problems/container-with-most-water/
// It's not easy to get the O(N) solution directly.
// The idea is to use this conclusion: if(height[i]<height[j]) then MaxArea(i,j)>=MaxArea(i+1,j)>=MaxArea(i,j-1), and vice versa
class Solution {
public:
int maxArea(vector<int> &height) {
int res=0;
int left=0, right=height.size()-1;
while(left<right){
res=max(res,min(height[left],height[right])*(right-left));
if(height[left]<height[right]) left++;
else right--;
}
return res;
}
};
本文介绍了一种高效的方法来解决LeetCode上的最大盛水量问题,通过使用双指针技巧优化时间复杂度至O(n),并详细解释了实现过程。
2833

被折叠的 条评论
为什么被折叠?



