leetcode Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) 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的更多相关文章
- LeetCode:Container With Most Water,Trapping Rain Water
Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...
- [LeetCode] Container With Most Water 装最多水的容器
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- [LeetCode]Container With Most Water, 解题报告
前言 难怪LeetCode OJ在找工作时被很多人推荐,发现了这道最大蓄水题目就是美团的笔试最后一道题,当时我霸笔只有着一道题目没有答出来,因此也就没有获得面试机会,可惜了 题目 Given n no ...
- C++LeetCode:: Container With Most Water
本来写的题目不是这个,而是字符串匹配,考虑了很多情况写了很久最后看了solution,发现可以用动态规划做.感觉被打击到了,果断先放着重新写一个题,后面心情好了再重新写吧,难过.每天都要被LeetCo ...
- [LeetCode] Container With Most Water 简要分析
前言 这题非要说贪心的话也算是吧,不过最主要的特征还是双指针.LC的题好像不少都是扔倆头尾指针然后遍历一遍完事儿的.这道题倒是“短板效应”的不错体现了. 题目 题目链接 Given n non-neg ...
- [Leetcode] Container With Most Water ( C++)
题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...
- LeetCode——Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- LeetCode Container With Most Water (Two Pointers)
题意 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai ...
- [Leetcode] container with most water 最大水容器
Given n non-negative integers a1 , a2 , ..., an , where each represents a point at coordinate (i, ai ...
随机推荐
- 与你相遇好幸运,Sails.js安装
官网: http://sailsjs.org Github:https://github.com/balderdashy/sails 开发文档: http://sailsjs.org/document ...
- 新手上路之Hibernate:第一个Hibernate例子
一.Hibernate概述 (一)什么是Hibernate? Hibernate核心内容是ORM(关系对象模型).可以将对象自动的生成数据库中的信息,使得开发更加的面向对象.这样作为程序员就可以使用面 ...
- c++ 的 static_cast
http://www.cnblogs.com/pigerhan/archive/2013/02/26/2933590.html #include "Person.h" #inclu ...
- 初识SQL Server Integration Service
SSIS(SQL Server Integration Service)是Microsoft 从SQL Server2005 以后发布的,现在一直跟随每个SQL server版本.它是Microsof ...
- ubuntu wubi非在线快速安装
最近ubuntu更新了,就想把它重新装回来试一下,但是由于种种原因划分磁盘不太方便,很自然就想到了wubi,这个不仅仅安全性高,而且比直接装系统快多了,而且方便.但是在线安装实在是太慢了,所以就找到了 ...
- 1.ok6410移植bootloader,移植u-boot,学习u-boot命令
ok6410移植u-boot 既然是移植u-boot当然首先需要u-boot源码,这里的u-boot代码是由国嵌提供的. 一.配置编译u-boot A. 解压 u-boot 压缩文件 B. 进入解压生 ...
- C[泊车管理系统]
// // main.c // 泊车管理系统 // // Created by 丁小未 on 13-7-14. // Copyright (c) 2013年 dingxiaowei. All ...
- 建造者模式与原型模式/builder模式与prototype模式/创建型模式
建造者模式 定义 用于简化复杂对象的创建 JDK中的建造者模式 java.lang.StringBuilder中的append()方法,每次调用后返回修改后的对象本身. public StringBu ...
- 通过jquery-qrcode在线生成二维码
随着移动互联网的发展,二维码现在应用得越来越广泛了,随手扫扫就可以浏览网站.加个好友什么的,比起手工输入真的是方便太多了. 前期做了一个综合测评系统,考虑逐步实现移动化,一长串的IP地址用户输入也不方 ...
- flex_高度补全
一个高度为100px, 另外一个高度自动补全当前界面下剩余的高度: <!DOCTYPE html> <html lang="en"> <head> ...