题目:

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. VC中窗口ID,句柄,指针三者相互转换函数【转】

    ID--HANDLE--HWND三者之间的互相转换id->句柄        hWnd = ::GetDlgItem(hParentWnd,id);id->指针        CWnd:: ...

  2. Jasper_crosstab_columngroup header position config - (headerPosition="Stretch")

    Position of Totals RowThe totalPosition attribute controls the appearance of the row that displays t ...

  3. npm install 本地安装与全局安装

    npm的包安装分为本地安装(local).全局安装(global)两种,从敲的命令行来看,差别只是有没有-g而已: npm install grunt # 本地安装 npm install -g gr ...

  4. java设计模式--结构型模式--外观模式

    外观模式 概述 为子系统中的一组接口提供一个一致的界面,Facade模式定义了一个高层接口,这个接口使得这一子系统更加容易使用. 适用性 1.当你要为一个复杂子系统提供一个简单接口时.子系统往往因为不 ...

  5. 【转】Android源码下载过程的一些注意事项

    原文网址:http://www.360doc.com/content/14/0113/11/11948835_344809459.shtml 其它一些事项说明: 1.在源代码下载过程中,我们在源代码下 ...

  6. Net-Snmp安装配置

    1. 下载安装 net-snmp安装程序:net-snmp-5.4.2.1-1.win32.exe Perl安装程序:ActivePerl-5.10.0.1004-MSWin32-x86-287188 ...

  7. Word Pattern II 解答

    Question Given a pattern and a string str, find if str follows the same pattern. Here follow means a ...

  8. Thinkphp将中文年份转换为数字年份的问题

    今天遇到一个问题:想将中文年份转换为数字年份,例如:"二零一六"-->'2016'. 在网上搜了一下,没找到可以直接处理的函数(也许是我搜索信息的能力有限吧>_< ...

  9. linux find命令-print0和xargs中-0使用技巧(转载)

    本文介绍了linux find命令中-print0和xargs中-0用法技巧,一些find命令的使用经验,需要的朋友参考下. 本节内容:linux find命令中-print0和xargs中-0的用法 ...

  10. ListView之BaseAdapter

    BaseAdapter可以实现自定义的丰富子项视图,本文实现如下所示结果: 实现代码: /* ListView :列表 BaseAdapter 通用的基础适配器 * * */ public class ...