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.

面试中比较常问的一道题,开始还以为是选择两个极大值点和一个极小值点,理解错了

第一种解法,直接暴力,时间复杂度O(n^2)

第二种解法,有点类似一次快排的过程

#include <iostream>
#include <vector>
#include <algorithm> using namespace std; int maxArea(vector<int> &height){
int maxWaterArea = ;
int left = , right = height.size()-;
while (left < right) {
maxWaterArea = max((right-left)*min(height[left],height[right]),maxWaterArea);
if (height[left] < height[right]) left++;
else right--;
}
return maxWaterArea;
}

关于如何证明最大值会出现在上述移动的方法中?可以用反证法证明。

当前首尾指针分别是i和j,其中A[i] < A[j],那么移动i。

假设有个k,在i和j之间,A[k]和A[i]组成容器所容纳的水最多,那么势必A[k]>A[j]

然而,A[i]和A[k]组成的容器的容量(k-i)*min(A[i],A[k]),即(k-i)*A[i],小于A[i]与A[j]所组成的容器大小:(j-i)*A[i]。

所有要移动i,而不是移动j

leetcode Container With Most Water的更多相关文章

  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 简要分析

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

  6. [Leetcode] Container With Most Water ( C++)

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

  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. Delphi中的异常处理

    转载:http://www.cnblogs.com/doit8791/archive/2012/05/08/2489471.html 以前写Delphi程序一直不注意异常处理,对其异常处理的机制总是一 ...

  2. 知乎大牛的关于JS解答

    很多疑惑一扫而空.... http://www.zhihu.com/question/35905242?sort=created JS的单线程,浏览器的多进程,与CPU,OS的对位. 互联网移动的起起 ...

  3. [LeetCode] TwoSum

    Given an array of integers, find two numbers such that they add up to a specific target number. The ...

  4. C#调用ArcGIS REST服务

    ArcGIS REST API提供了简单.开放的接口来访问和使用ArcGIS Server发布的服务.使用ArcGIS REST API通过URL可以获取和操作每一个服务中的所有资源和操作. 1.使用 ...

  5. POJ3294 Life Forms(后缀数组)

    引用罗穗骞论文中的话: 将n 个字符串连起来,中间用不相同的且没有出现在字符串中的字符隔开,求后缀数组.然后二分答案,用和例3 同样的方法将后缀分成若干组,判断每组的后缀是否出现在不小于k 个的原串中 ...

  6. Async/Await 最佳实践

    其实好久以前就看过这个文章,以及类似的很多篇文章.最近在和一个新同事的交流中发现原来对async的死锁理解不是很透彻,正好最近时间比较充裕就再当一回搬运工. 本文假定你对.NET Framework ...

  7. iOS 随记

    UIImage 走缓存 + (UIImage *)imageNamed:(NSString *)name; 不走缓存 + (UIImage *)imageWithContentsOfFile:(NSS ...

  8. bzoj 1415 期望+记忆化搜索 ****

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAdkAAAIfCAIAAACzfDFhAAAgAElEQVR4nOy9bVwTW57vm5fnhed+Pn

  9. HR外包系统 - 客户员工 发薪需求/个税需求 设置

    最好,客户公司层面进行设置,如果单一情况,只需要设置公司,如果不是单一情况,设置员工, 另外员工只能从公司设置好的地方选择过来. 增强系统简便设置和设置的灵活性.

  10. LoadRunner编程之跳出迭代【exit(-1)和return 0】

    只运行了一次迭代,就结束了. LR脚本实践:关于lr中exit(-1)和return 0的区别 exit(-1):从当前action里面exit(-1)所在行,当前迭代里面直接退出来,终止运行:   ...