Leetcode题目:Container With Most Water

题目:

Given n non-negative integers a1a2, ..., an, where each represents a point at coordinate (iai). n vertical lines are drawn such that the two endpoints of line i is at (iai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.

Note: You may not slant the container.

题目解答:

  题目中要求得两个纵轴与x轴围起来的容器的最大容积。这里使用两个指针,从头尾两边向中间缩进。不断更新最值。

代码:

class Solution {
public:
    int maxArea(vector<int>& height) {
        vector<int>::iterator vleft = height.begin();
        vector<int>::iterator vright = height.end() - 1;
        int res = 0;
        while(vleft != vright)
        {
           // cout << "leftv" <<*vleft <<endl;
            //cout << "rightv"<<*vright <<endl;
            //cout << "minus" << (vright - vleft) << endl;
			res = max(res, min(*vleft, *vright) * (vright - vleft) );
            //cout << "res" << res << endl; 

            if(*vleft < *vright)
            {
                vector<int>::iterator tmp = vleft;
                while( (tmp != vright) && (*tmp <= *vleft) )
                {
                    tmp++;
                }
                vleft = tmp;
            }
            else
            {
                vector<int>::iterator tmp = vright;
                while( (tmp != vleft) && (*tmp <= *vright) )
                {
                    tmp--;
                }
                vright = tmp;
            }
        }
        return res;
    }
    int max(int a,int b)
    {
        return a > b ? a : b;
    }

    int min(int a,int b)
    {
        return a < b ? a : b;
    }
};

  

时间: 2024-10-24 06:58:57

Leetcode题目:Container With Most Water的相关文章

LeetCode 011 Container With Most Water

[题目] Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a con

【LeetCode】Container With Most Water

Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a containe

[LeetCode][JavaScript]Container With Most Water

Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together wi

leetcode之Container With Most Water 和Trapping Rain Water

Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together wi

LeetCode OJ Container With Most Water 容器的最大装水量

题意:在坐标轴的x轴上的0,1,2,3,4....n处有n+1块木板,长度不一,任两块加上x轴即可构成一个容器,其装水面积为两板的间距与较短板长之积,以vector容器给出一系列值,分别代表在0,1,2,3,4...vector.size()-1共size个短板,是连续的,不排除有板长为0的可能性,但是至少有两块板.根据所给板长,求任两板与x轴所能构成的最大面积并返回. 代码: 1 class Solution { 2 public: 3 int maxArea(vector<int> &am

Leetcode:Container With Most Water

转载请注明本文地址:http://blog.csdn.net/yangnanhai93 Leetcode题目链接地址:https://leetcode.com/problems/container-with-most-water/ 暴力的方式还是很简单的,O(n^2)可以完成,任意两个的组合都是可以解决这个问题的,但是,仔细想想,可以发现,题目还是有规律的. 算法的思路是选择最外围的两个挡板,然后去除一个短的,如此递归,即可完成计算: 证明: 假设,存在挡板A.B,A->height<B.he

Leetcode 贪心 container with most water

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Container With Most Water Total Accepted: 15241 Total Submissions: 48936My Submissions Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertica

LeetCode 11. Container With Most Water (装最多水的容器)

Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a containe

[LeetCode] 11. Container With Most Water 装最多水的容器

Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a containe

LeetCode 11 Container With Most Water(最大水容器)

翻译 给定n个非负整数a1,a2,...,an,其中每个代表一个点坐标(i,ai). n个垂直线段例如线段的两个端点在(i,ai)和(i,0). 找到两个线段,与x轴形成一个容器,使其包含最多的水. 备注:你不必倾倒容器. 翻译 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such tha