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.

思路:每次移动矮的那侧的指针,并且只关心比原来高的情况(因为宽度已经减小了,高度必须增高才可能面积变大)

int maxArea(int* height, int heightSize) {
int start = ;
int end = heightSize-;
int ret = ;
int maxStart = ;
int maxEnd = ; while(start < end){
if(height[start] < height[end]){
if(height[start]*(end-start) > ret) ret = height[start]*(end-start);
while(start<end){
start++;
if(height[start] > maxStart){
maxStart = height[start];
break;
}
}
}
else{
if(height[end]*(end-start) > ret) ret = height[end]*(end-start);
while(start<end) {
end--;
if(height[end] > maxEnd){
maxEnd = height[end];
break;
}
}
}
} return ret;
}

11. Container With Most Water(头尾双指针)的更多相关文章

  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. 11. Container With Most Water(装最多的水 双指针)

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

  9. LeetCode 11. Container With Most Water (装最多水的容器)

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

随机推荐

  1. groovy 从jsonList中读取某个字段

    今天又被groovy的高效吓到了. 想提取所有的itemCodes,两种玩法 一.常规方法:遍历组装 RestResult items = getListPager() def temp = [] i ...

  2. 浅谈 CAS

    CAS: CAS:Compare and Swap, 翻译成比较并交换. java.util.concurrent包中借助CAS实现了区别于 synchronized 同步锁的一种乐观锁. CAS应用 ...

  3. DOS 格式化日期时间输出

    if "%date:~5,2%" lss "10" (set mm=0%date:~6,1%) else (set mm=%date:~5,2%)if &quo ...

  4. python中pop()函数的用法

    pop() 函数用于移除列表中的一个元素(默认最后一个元素),并且返回该元素的值. 语法:list.pop(obj=list[-1]) //默认为 index=-1,删除最后一个列表值. obj -- ...

  5. Delphi 三层TDataSetProvider

    在Delphi想使用三层架构或者使用TClientDataSet控件,一般都需要引用TDataSetProvider控件,现对TDataSetProvider控件的Options属性值做一个简单的分析 ...

  6. 各种Queue分析

    Queue主要方法的区别:   抛出异常 返回特殊值 插入 add(e)插入成功则返回true,没有可用空间则IllegalStateException offer(e) 移除 remove(e)获取 ...

  7. php 分页实现 和 php 大文件上传失败的处理方式

    1. php分页: 要想做php的分页,首先要弄清楚要什么样的分页,关系到哪些参数,参数之间怎么关联: 目标分页效果如下: 分析: 1.主要涉及两个参数:一个是当前输出页面的页码,用变量$p 表示,另 ...

  8. Oracle VM VirtualBox各种显示模式切换 热键

    初用VirtualBox, 几个显示切换快捷键还是要记一下的: Right Ctrl + F        -- 切换到全屏模式 Right Ctrl + L        -- 切换到无缝模式 Ri ...

  9. [Linux]CentOS7搭建Nginx + MySQL + PHP

    ------------------------------------------------------------------------------------- Nginx安装参考地址:ht ...

  10. [CI]CodeIgniter视图 & 模型 & 控制器

    ---------------------------------------------------------------------------------------------------- ...