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. ListView 完全优化 + 多种listitem布局处理

    #  百度了下,感觉下面的博客文章还都挺全面的,写的很好,直接分享得了 Android性能优化--Listview优化 - tonycheng93 - 博客园http://www.cnblogs.co ...

  2. Pyqt 时时CPU使用情况

    借鉴代码来自:https://github.com/hgoldfish/quickpanel 实现代码: # -*- coding:utf-8 -*- from __future__ import p ...

  3. android 入门-库的生成jar 和引用jar

    开发环境 1.eclipse 2.android studio 步骤 1.在eclipse 生成 jar包 2.在android studio 引用 jar包 实现步骤 1.在eclipse 创建一个 ...

  4. 把浏览器的私有模式添加到VS中

    题记:在用VS进行Web开发的时候,常常希望VS的调试不会对浏览器造成固定的影响,那么使用浏览器的私有模式来启动就很有必要. 前几天SCOTT HANSELMAN分享了一个开发Web应用程序的小技巧, ...

  5. Convert XML to Object using LINQ

    Class and Xml : Please see my another article. http://www.cnblogs.com/mingmingruyuedlut/p/3436803.ht ...

  6. Database.com SOQL and SOSL Reference

    如下是关于 SOQ L与 SOSL 的相关链接: http://docs.database.com/dbcom/en-us/db_sosl_soql/sforce_api_calls_soql.htm ...

  7. UVA 11987 Almost Union-Find (并查集+删边)

    开始给你n个集合,m种操作,初始集合:{1}, {2}, {3}, … , {n} 操作有三种: 1 xx1 yy1 : 合并xx1与yy1两个集合 2 xx1 yy1 :将xx1元素分离出来合到yy ...

  8. WinForm使用WebBrowser操作HTML页面的Element

    前言 在 Window Form 应用程序如果需要浏览网页时可以崁入 WebBrowser 控件,但如果需要操作崁入的 HTML 的网页元素,就需要额外的操作,以下纪录几种操作 HTML 元素的方法以 ...

  9. caffe中添加local层

    下载caffe-local,解压缩; 修改makefile.config:我是将cuudn注释掉,去掉cpu_only的注释; make all make test(其中local_test出错,将文 ...

  10. jquery replace用法汇总

    //只替换匹配到的第一个目标 var str="Visit Microsoft! Microsoft"document.write(str.replace(/Microsoft/, ...