题目:

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.

Tag:

Array; Two Pointers

体会:

1. 这道题我觉得是双指针的一个创新用法。之前的双指针都是一主一辅,辅助的那个不断去探测下一个,然后主要的那个会根据探测结果做出相应动作,比如 Merge Sorted Array,这道题的双指针两个人是同等重要性,根据其他的判定条件来决定下一次移动谁。

2. 这道题之所以是双指针是同等位置,也可以从另外一个角度来感受,就是要知道左边那条线“和”右边那条线的位置,两个都是未知的,都是要确定的。

3. 回到题目上,O(N)的解法。代码很简单,可是能想到不容易。(我也是炒的别人的思路)。为什么每次是那样移动指针呢?假设h[left] < h[right],我们管当前用来围城面积的左边的这条竖着的直线叫Line left, 管右边的那条线要Line right。那么当前的面积就是 (area = (right -  left) * h[left])。好了,我们假设还有一个更大的面积,下面我们要说明这个更大的面积一定不会是和Line left围成的。

假设这个更大的面积的其中一条边是Line x, (Line x肯定是位于Line left 和Line right之间啦)。那么Line和max围成的面积是( ( x - left) * h[left]) , 这个面积肯定是小于原来的 (right - left ) * h[left]的啦。

综上,我们肯定是要移动Line left了。

4. 可以从一个更数学的角度来解释这个问题:

  假设left, right 是line left和line right在x轴上的坐标,起始时, left = 0, right = 最右边。那么

  area = ( right - left ) * min (h[left], h[right])

  怎么样才能使这个函数有更大的值呢?不管移动左边的线还是右边的线,结果都是(right - left)的值会变小,若要area的值变大,只能是把min(h[left], h[right])的值变大才有可能。所以如果我们继续保留较矮的那条线的话,这个min值一定不会变大,所以面积肯定不会变大;而保留原本较高的那条线,是有可能使min值变大的,所以只能是保留这条线。

 class Solution {
public:
int maxArea(vector<int> &height) {
int left = ;
int right = height.size() - ;
int result = ;
int area = ;
while (left < right) {
if (height[left] < height[right]) {
area = (right - left) * height[left++];
} else {
area = (right - left) * height[right--];
}
if (area > result) {
result = area;
}
}
return result;
}
};

[Leetcode] Container With Most Water ( C++)的更多相关文章

  1. LeetCode:Container With Most Water,Trapping Rain Water

    Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...

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

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...

  3. [LeetCode]Container With Most Water, 解题报告

    前言 难怪LeetCode OJ在找工作时被很多人推荐,发现了这道最大蓄水题目就是美团的笔试最后一道题,当时我霸笔只有着一道题目没有答出来,因此也就没有获得面试机会,可惜了 题目 Given n no ...

  4. C++LeetCode:: Container With Most Water

    本来写的题目不是这个,而是字符串匹配,考虑了很多情况写了很久最后看了solution,发现可以用动态规划做.感觉被打击到了,果断先放着重新写一个题,后面心情好了再重新写吧,难过.每天都要被LeetCo ...

  5. leetcode Container With Most Water

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).  ...

  6. [LeetCode] Container With Most Water 简要分析

    前言 这题非要说贪心的话也算是吧,不过最主要的特征还是双指针.LC的题好像不少都是扔倆头尾指针然后遍历一遍完事儿的.这道题倒是“短板效应”的不错体现了. 题目 题目链接 Given n non-neg ...

  7. LeetCode——Container With Most Water

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).  ...

  8. LeetCode Container With Most Water (Two Pointers)

    题意 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai ...

  9. [Leetcode] container with most water 最大水容器

    Given n non-negative integers a1 , a2 , ..., an , where each represents a point at coordinate (i, ai ...

随机推荐

  1. [Leetcode] Remove Duplicates From Sorted Array II (C++)

    题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...

  2. Mysql学习(慕课学习笔记2)数据库的创建与删除

    创建数据库 { } 必选  | 从前后做选择   [ ] 可选 Create {database | schema} [if not exists] db_name [default] charact ...

  3. python中 and 和 or 运算的核心思想 ——— 短路逻辑

    python中 and 和 or 运算的核心思想 --- 短路逻辑 1. 包含一个逻辑运算符 首先从基本的概念着手,python中哪些对象会被当成 False 呢?而哪些又是 True 呢? 在Pyt ...

  4. Strange Grid

    def main(): r,c = map(int, raw_input().split(' ')) if r % 2 != 0: base = 5*(r-1) else: base = 5*(r-2 ...

  5. 分治算法求乘方a^b 取余p(divide and conquer)

    传统的计算方法为循环n个a相乘.时间复杂度为O(n). 如用分治算法,效率可提升至O(lgn). 结合recursive有 double pow(int a, int n){ ) ; ) return ...

  6. scaletype

    http://www.myexception.cn/image/726203.html 图片说明Andorid中ImageView的不同属性ScaleType的区别 ImageView是Android ...

  7. Qt编程之信号与槽-------unresolved external symbol "public: virtual struct QMetaObject const * __thiscall XX::metaObject(void)const

    原因是加入Q_OBJECT这个macro的类,被编译的时候就要用到moc这个命令,所以在VS2010中,没有加入此命令的应用,当然会出错了.所以解决办法是加,或者如果你不使用信号槽可以直接删除. 当要 ...

  8. wikioi1369 xth 砍树

    题目描述 Description 在一个凉爽的夏夜,xth 和 rabbit 来到花园里砍树.为啥米要砍树呢?是这样滴, 小菜儿的儿子窄森要出生了.Xth这个做伯伯的自然要做点什么.于是他决定带着 r ...

  9. Asp.net MVC 3 防止 Cross-Site Request Forgery (CSRF)原理及扩展 安全 注入

    原理:http://blog.csdn.net/cpytiger/article/details/8781457 原文地址:http://www.cnblogs.com/wintersun/archi ...

  10. KDTree详解及java实现

    本文内容基于An introductory tutoril on kd-trees 1.KDTree介绍 KDTree根据m维空间中的数据集D构建的二叉树,能加快常用于最近邻查找(在加快k-means ...