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. ora-01658 :无法为表空间USERS 中的段创建INITIAL区

    "CREATE INDEX "IDX_TS_BONUS_Q_201209_DS" ON "TS_BONUS_Q_201209" ("DS&q ...

  2. sqlplus使用(一)

    一,sqlplus 环境变量 sqlplus的环境变量(来自SQL*Plus® User's Guide and Reference Release 11.2) Parameter or Variab ...

  3. C#路径/文件/目录/I/O常见操作汇总

    文件操作是程序中非常基础和重要的内容,而路径.文件.目录以及I/O都是在进行文件操作时的常见主题,这里想把这些常见的问题作个总结,对于每个问题,尽量提供一些解决方案,即使没有你想要的答案,也希望能提供 ...

  4. C# SMTP邮件发送 分类: C# 2014-07-13 19:10 333人阅读 评论(1) 收藏

    邮件发送在网站应用程序中经常会用到,包括您现在看到的博客,在添加评论后,系统会自动发送邮件通知到我邮箱的,把系统发送邮件的功能整理了下,做了一个客户端Demo,希望对有需要的童鞋有所帮助: 核心代码: ...

  5. usb设备驱动描述,王明学learn

    usb设备驱动 本章主要内容包含以下:USB总线介绍,USB协议分析,USB系统架构 一.USB总线介绍 1.1USB发展史 USB(Universal Serial Bus)通用串行总线,是一种外部 ...

  6. Objective-C编码规范:26个方面解决iOS开发问题(转)

    链接

  7. orientation和gravity的区别

    orientation:决定UI组件是按行还是列显示 gravity:指定文字对齐方式.

  8. 【转】从RGB色转为灰度色算法

    ----本文摘自作者ZYL910的博客 一.基础  对于彩色转灰度,有一个很著名的心理学公式: Gray = R*0.299 + G*0.587 + B*0.114 二.整数算法 而实际应用时,希望避 ...

  9. JQuery Event属性说明

    JQuery事件中的Event属性是经常性的被忽略的.大多数时间你的确不怎么用它,但有些时候它还是有作用的. 如获知触发时用户的环境(是否按了shift etc).每个浏览器对event都有不同的地方 ...

  10. 【Web前端】---js调用本地应用程序

    最近进入了一个项目组,向大牛们一起学习如何搞开发,可谓是边开发边学习.就在前两天,我们的项目被领导们验收了一次,顺便给我们提了点新的需求,要求我们能够使用外在设备拍照上传.君要臣死,臣不能不死.更何况 ...