一天一道LeetCode系列

(一)题目

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)。

那么我们可以寻找O(n)的解法。

其实也比较简单,定义两个指针,分别指向头和尾,由于容易装水的容量是由小端决定,所以指针移动的准则就是小端指针移动。

只需要遍历一遍就能得到最大值。

class Solution {
public:
    int maxArea(vector<int>& height) {
        int i = 0;
        int j = height.size()-1;
        int max=0;
        while(j>i)
        {
            int con = 0;
            int min = height[i]>height[j]?height[j]:height[i];
            if(j==i+1){con = min;}
            else
                con = min *(j-i);
            if(height[i]>height[j])
            {
                j--;
            }
            else i++;
            if(con>max) max=con;
        }
        return max;
    }
};

【一天一道LeetCode】#11Container With Most Water的更多相关文章

  1. 【一天一道LeetCode】#42. Trapping Rain Water

    一天一道LeetCode系列 (一)题目 Given n non-negative integers representing an elevation map where the width of ...

  2. 【一天一道LeetCode】索引目录 ---C++实现

    [一天一道LeetCode]汇总目录 这篇博客主要收藏了博主所做题目的索引目录,帮助各位读者更加快捷的跳转到对应题目 目录按照难易程度:easy,medium,hard来划分,读者可以按照难易程度进行 ...

  3. leetcode#42 Trapping rain water的五种解法详解

    leetcode#42 Trapping rain water 这道题十分有意思,可以用很多方法做出来,每种方法的思想都值得让人细细体会. 42. Trapping Rain WaterGiven n ...

  4. [array] leetcode - 42. Trapping Rain Water - Hard

    leetcode - 42. Trapping Rain Water - Hard descrition Given n non-negative integers representing an e ...

  5. 【一天一道LeetCode】#303.Range Sum Query - Immutable

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 我的个人博客已创建,欢迎大家持续关注! 一天一道le ...

  6. 【一天一道Leetcode】#190.Reverse Bits

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 我的个人博客已创建,欢迎大家持续关注! 一天一道le ...

  7. 【一天一道Leetcode】#203.Remove Linked List Elements

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 我的个人博客已创建,欢迎大家持续关注! 一天一道le ...

  8. 【一天一道LeetCode】#290. Word Pattern

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  9. 【一天一道LeetCode】#219. Contains Duplicate II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

随机推荐

  1. Programming In Scala笔记-第四章、类和对象

    类似于Java,Scala中也有类和对象的概念. 一.类.属性和方法 1.类 类是对一类事物的抽象,当一个类被定义后,就可以以该定义为模板,定义该类的一系列对象.比如说有以下一个模板 人类: 有姓名: ...

  2. 前端CSS技术全解(二)

    欢迎转载,转载请标明出处: http://blog.csdn.net/johnny901114/article/details/52813761 本文出自:[余志强的博客] 一.CSS三大特性 1)继 ...

  3. Android图表库MPAndroidChart(六)——换一种思考方式,水平条形图的实现过程

    Android图表库MPAndroidChart(六)--换一种思考方式,水平条形图的实现过程 一.基本实现 我们之前实现了条形图,现在来看下水平条形图是怎么实现的,说白了就是横起来,看下效果: 说起 ...

  4. android开发中使用到的一些设计者模式

    单例模式 概念:确保一个类只有一个实例,并且自行实例化并向整个系统提供整个实例. public class Singleton { private static volatile Singleton ...

  5. 搭建ejabberd集群

    搭建ejabberd集群(金庆的专栏 2016.8)以2台机器搭建一个ejabberd集群.2台机器都是外网一块网卡,内网另一块网卡.新建一个域名,添加2台机器的外网IP.分别用源码安装ejabber ...

  6. JAVA面向对象-----匿名内部类

    匿名内部类 匿名内部类:就是没有类名字的内部类. 匿名内部类作用:简化内部类书写. 匿名内部类的前提:必须继承一个父类或者是实现一个接口. 匿名内部类的格式: new 父类或者接口(){ 执行代码-. ...

  7. Scikit-learn:数据预处理Preprocessing data

    http://blog.csdn.net/pipisorry/article/details/52247679 本blog内容有标准化.数据最大最小缩放处理.正则化.特征二值化和数据缺失值处理. 基础 ...

  8. 最优化方法:范数和规则化regularization

    http://blog.csdn.net/pipisorry/article/details/52108040 范数规则化 机器学习中出现的非常频繁的问题有:过拟合与规则化.先简单的来理解下常用的L0 ...

  9. SQLite AND/OR 运算符(http://www.w3cschool.cc/sqlite/sqlite-and-or-clauses.html)

    SQLite AND/OR 运算符 SQLite 的 AND 和 OR 运算符用于编译多个条件来缩小在 SQLite 语句中所选的数据.这两个运算符被称为连接运算符. 这些运算符为同一个 SQLite ...

  10. (一三〇)UITextField的光标操作扩展

    简介 在iOS开发中,有时候需要完全自主的定义键盘,用于完整的单词输入,例如计算机应用中,需要一次性的输入sin(,在移动光标时要完整的跳过sin(,在删除时也要完整的删除,这就需要对光标的位置进行精 ...