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.

思路:两个指针指向开头和结尾的位置,每次将矮的那侧的指针往中间移动,并且只关心比原来高的情况(因为宽度已经减小了,高度必须增高才可能面积变大)

class Solution {
public:
int maxArea(vector<int>& height) {
int size = height.size();
int left = , right = height.size()-;
int maxLeft = , maxRight = ;
int ret = ; while(left < right){
if (height[left] < maxLeft) {
left++;
continue;
}
else maxLeft = height[left];
if (height[right] < maxRight) {
right--;
continue;
}
else maxRight = height[right];
ret = max(ret, min(height[left], height[right]) * (right - left)); if(height[left] < height[right]) left++;
else right--;
}
return ret;
}
};

11.Container With Most Water (Array; Two-Pointers)的更多相关文章

  1. leetcode 11. Container With Most Water 、42. Trapping Rain Water 、238. Product of Array Except Self 、407. Trapping Rain Water II

    11. Container With Most Water https://www.cnblogs.com/grandyang/p/4455109.html 用双指针向中间滑动,较小的高度就作为当前情 ...

  2. LeetCode Array Medium 11. Container With Most Water

    Description Given n non-negative integers a1, a2, ..., an , where each represents a point at coordin ...

  3. Leetcode 11. Container With Most Water(逼近法)

    11. Container With Most Water Medium Given n non-negative integers a1, a2, ..., an , where each repr ...

  4. 刷题11. Container With Most Water

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

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

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

  6. [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: 找出数组中最大的数 ...

  7. 《LeetBook》leetcode题解(11):Container With Most Water[M] ——用两个指针在数组内移动

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  8. Leetcode Array 11 Container With Most Water

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

  9. LeetCode 11. [👁] Container With Most Water & two pointers

    盛最多水的容器 给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找 ...

随机推荐

  1. bzoj2669 [cqoi2012]局部极小值 状压DP+容斥

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=2669 题解 可以发现一个 \(4\times 7\) 的矩阵中,有局部最小值的点最多有 \(2 ...

  2. ubuntu 解压zip 文件乱码

    用 unzip -O cp936 就可以了, 但是,有些发行版所带的 unzip 没有这个参数,比如 ArchLinux 就需要安装 unzip-iconv. Ubuntu 12.04 里面的 unz ...

  3. 9.Markdown语法(自用)——2019年12月12日

    title: markdown语法说明 date: "2018-12-26 20:17:16" tags: 技术指令 categories: 技术驿站 markdown语法说明 2 ...

  4. VS2005下使用GSL-1.15小结

    最近在复习高等数学,有时为了验证顺便复习下C语言,看了看自己下载收集的软件,发现C语言有一个数学工具包,是GNU开发的,叫做GSL--GNU Scientific Library,中文:C++科学计算 ...

  5. android android studio error

    SIMPLE: Error computing //cmake 包含的跨平台头文件或者是源文件路径出错

  6. mobx学习笔记04——mobx常用api

    1 可观察的数据(observable) observable是一种让数据的变化可以被观察的方法. 那些数据可被观察? -原始类型 String.Number.Boolean.Symbol -对象 - ...

  7. 第一周作业—N42-虚怀若谷

    一.Linux发行版描述. Linux发行版主要有三个分支:Slackware.Debian.Redhat: (1) Slackware: SUSE:基于Slackware二次开发的一款Linux,主 ...

  8. shell脚本学习(6)awk 编排字段

    awk能取出文本字段重新编排 1 awk的用法 awk ‘program’ [file] 2 其中program 可以写成 ‘parrtern {action}’    pattern 或 actio ...

  9. PHP array_change_key_case() 函数

    实例 将数组的所有的键转换为大写字母: <?php $age=array("Peter"=>"35","Ben"=>&qu ...

  10. vue使用中的问题总结

    1.根实例问题 vue中的根实例可以有多个,每个根实例可以挂载DOM元素,只有在挂载的DOM元素上才可以使用该实例中的数据方法等. 并且,组件只有在某一个根实例所挂载的DOM元素上才可以使用. 2.组 ...