Leetcode: Container With Most Water

原创 2016年08月31日 16:36:44
class Solution {
public:
    /*
    贪心:
    从长度最长开始,即left=0,right=height.size()-1
    每次更新宽度小的那个边界,left向右移动,right向左移动
    同步更新答案 即area 
    */
    int maxArea(vector<int>& height) {
        int n=height.size();
        int left=0;
        int right=n-1;
        int max_area=(right-left)* min(height[left],height[right]);
        while(left<right){
            if(height[left]<height[right]){
                left++;
            }
            else{
                right--;
            }
            int max_=(right-left)* min(height[left],height[right]);
            max_area=max(max_,max_area);
        }
        return max_area;
    }
};

版权声明:本文为博主原创文章,欢迎指教~

相关文章推荐

Leetcode Q11:Container With Most Water

题目11: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (...

LeetCode_Container With Most Water

Container With Most Water   Given n non-negative integers a1, a2, ..., an, where each repr...

【LeetCode】Container With Most Water

参考链接 题目描述 题目分析 总结 代码示例 推荐学习C++的资料 C++标准函数库 http://dow...

LeetCode | Container With Most Water

题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinat...

LeetCode011 Container With Most Water

详细见:leetcode.com/problems/container-with-most-water/ Java Solution: github package leetcode;...

LeetCode11. Container With Most Water

11. Container With Most Water 1、原题 Given n non-negative integers a1, a2, ..., an, where each...

LeetCode11. Container With Most Water(思维题:选择左右边使得容器所盛水最多)

这道题,我独立想了好久!!!!一道很好的思维题!!!!!!!! 思路1是自己的求解方法O(nlogn),思路2是网上最优的解法O(n),暴利O(n^2)肯定超时! 题目链接:htt...

LeetCode: Container With Most Water [Java]

Description: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordi...

LeetCode-Container With Most Water

题目给定一个正数数组,数组中的第i个元素ai表示一个点坐标(i, ai),每个点对应一条直线(i, 0)。找出两条直线,使它们与x轴构成一个容器。找到一个容器,是该容器中能装的水比其他容器都多。 ...

[LeetCode]11.Container With Most Water

【题目】 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)