leetcode-011:Container With Most Water
2013-09-02 22:15
274 查看
class Solution { public: int maxArea(vector<int> &height) { // Start typing your C/C++ solution below // DO NOT write int main() function int result=0; if(height.size()<=1) return result; int i=0,j=height.size()-1; while(i<j) { result=max(result,(j-i)*min(height[i],height[j])); if(height[i]<height[j]) i++; else j--; } return result; } };
初始思路,任意的i,j 计算
优化思路,如果height[i]<height[j],则i-1和j 一定比之前的最大值小,没有必要进行计算;同理,右边。
相关文章推荐
- 【LeetCode】011 Container With Most Water
- 【LeetCode-面试算法经典-Java实现】【011-ContainerWithMostWater(容纳最多的水)】
- [LeetCode]-011-Container_With_Most_Water
- LeetCode 011 Container With Most Water
- [LeetCode]题解(python):011-Container With Most Water
- 011_LeetCode_11 Container With Most Water 题解
- LeetCode 011 Container With Most Water
- LeetCode 011: Container With Most Water
- leetcode 011 —— Container With Most Water
- LeetCode-011 Container With Most Water
- [LeetCode]-011-Container With Most Water
- LeetCode-011 Container With Most Water
- LeetCode 011 Container With Most Water
- leetcode解题方案--011--Container With Most Water
- 【JAVA、C++】LeetCode 011 Container With Most Water
- leetcode--011 Container With Most Water
- [LeetCode]011-Container With Most Water
- 【LeetCode】011 Container With Most Water
- leetcode011 Container With Most Water
- LeetCode 011 Container With Most Water