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.

思路: 贪婪。从两端开始,计算面积。每次走一步,若左端高度较低,从左端走;反之,从右端走。

class Solution {
public:
int maxArea(vector<int> &height) {
int maxS = 0;
int it1 = 0, it2 = height.size()-1;
while(it1 < it2){
int S = min(height[it1], height[it2]) * (it2 - it1);
if(S > maxS) maxS = S;
if(height[it1] >= height[it2]) --it2;
else ++it1;
}
return maxS;
}
};

67. Container With Most Water的更多相关文章

  1. 刷题11. Container With Most Water

    一.题目说明 11.Container With Most Water,这个题目难度是Medium. 二.我的做法 乍一看,简单啊,两个for循环就可以了,我在本地写的. #include<io ...

  2. 如何装最多的水? — leetcode 11. Container With Most Water

    炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...

  3. [LintCode] Container With Most Water 装最多水的容器

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

  4. LeetCode:Container With Most Water,Trapping Rain Water

    Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...

  5. No.011 Container With Most Water

    11. Container With Most Water Total Accepted: 86363 Total Submissions: 244589 Difficulty: Medium Giv ...

  6. leetcode面试准备:Container With Most Water

    leetcode面试准备:Container With Most Water 1 题目 Given n non-negative integers a1, a2, ..., an, where eac ...

  7. 关于Container With Most Water的求解

    Container With Most Water 哎,最近心情烦躁,想在leetcode找找感觉,就看到了这题. 然而,看了题目半天,硬是没看懂,于是乎就百度了下,怕看到解题方法,就略看了下摘要,以 ...

  8. [leecode]---11.container with most water

    description: Input: [1,8,6,2,5,4,8,3,7]Output: 49 思路1: 从(1,a1)开始向后算面积,需要两层n循环,时间复杂度n2 思路2: 找出数组中最大的数 ...

  9. Leetcode11 Container With Most Water 解题思路 (Python)

    今天开始第一天记录刷题,本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 准备按tag刷,第一个tag是array: 这个是array的第一道题:11. Container With ...

随机推荐

  1. Yii2-redis

    安装:composer require --prefer-dist yiisoft/yii2-redisredis 版本 >= 2.6.12 添加配置: 'components' => [ ...

  2. 安装64位mysql5.626

    计算机--右击属性--左上高级系统变量---环境变量 path 添加 mysql 的bin目录 ;D:\mysqlwinx64\bin1 //mysql 5.6.26安装前先解压到d盘根目录 cd D ...

  3. 大量无线键盘存在KeySniffer漏洞-可嗅探用户输入的内容

    几美元的一根天线.一个无线发射器,还有几行Python代码,有了这些,黑客就可以在几十米开外主动地记录下你的用户名.密码.信用卡.你写的稿子,总之就是你用无线键盘输入的任何东西. 黑客所利用的是一种无 ...

  4. sublime福音:微信小程序组件及API补全插件

    微信自带的编辑器操作起来各种不顺手,调试的时候需要用到,但是编辑的时候还是用自己熟悉的编辑器好一点. 将文件目录导入到sublime,在sublime编辑保存后,回到小程序开发工具刷新页面即可. 下面 ...

  5. 多线程下HttpContext.Current 的问题

    在项目中需要记录文本日志,为了加快响应速度所以用到了多线程. 但是以前的方法是不支持多线程的,程序运行错误. 追踪代码发现提示HttpContext为空. 1.HttpContext.Current表 ...

  6. Node.js高级编程读书笔记 - 5 数据库 - Never

    Outline 6 连接数据库 6.1 使用node-mysql连接MySQL数据库 6.2 使用Nano连接CouchDB数据库 6.3 使用Mongoose连接MongoDB数据库 6 连接数据库 ...

  7. 【Gerrit】gerrit server搭建

    Part 1  Gerrit Prerequisites: 1.Java JDK>1.7 2.Git 3.SSH server 4.DB part 2 Set local gerrit serv ...

  8. Python 学习之 NumPy

    NumPy(Numerical Python的简称) 是高性能科学计算和数据分析的基础包,提供了矩阵运算的功能,其一般与Scipy.matplotlib一起使用.其实,list已经提供了类似于矩阵的表 ...

  9. HTML5实战与剖析之触摸事件(touchstart、touchmove和touchend)

    HTML5中新添加了很多事件,但是由于他们的兼容问题不是很理想,应用实战性不是太强,所以在这里基本省略,咱们只分享应用广泛兼容不错的事件,日后随着兼容情况提升以后再陆续添加分享.今天为大家介绍的事件主 ...

  10. 组合模式(Composite Pattern)

    组合模式主要用来处理一类具有“容器特征”的对象——即它们在充当对象的同时,又可以作为容器包含其他多个对象. 组合模式实现的最关键的地方是——简单对象和复合对象必须实现相同的接口.这就是组合模式能够将组 ...