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. <面试> PHP 常见算法

    排序算法 1. 冒泡排序(数组排序) 基本思想:对需要排序的数组从后往前(逆序)进行多遍的扫描,当发现相邻的两个数值的次序与排序要求的规则不一致时,就将这两个数值进行交换.这样每遍历一次,最小的数值就 ...

  2. js 深度拷贝

    js 数据类型 分为2种: 基本数据类型:Undefined.Null.Boolean.Number.String 复杂数据类型:Object.Array.function 他们的区别是在内存中的存储 ...

  3. Redis 发布与订阅模式

    subscribe  订阅 publish   发布   频道    发布内容

  4. beego注解路由 [自定义方法]

    背景: beego生成的controller里面,默认get请求到由Get()方法处理:post请求由Post()方法处理 etc. 如果想自定义方法来处理请求,改怎么做? 直接拿beego的文档来说 ...

  5. hadoop配置2.6.1 centos7

    上传文件(分发)的三种方式: 1.本地: -file 的模式,上传一些小的文件. 例如: -file ./test INPUT_FILE_PATH_1="/The_Man_of_Proper ...

  6. keal

    I remember the wonderful moment you appeared before me, like a fleeting vision, like a genius of pur ...

  7. dockerfile构建nginx并结合php

    转自:https://www.cnblogs.com/jsonhc/p/7799358.html 查看nginx和php的目录结构: [root@docker docker_demo]# tree n ...

  8. centos7,进程最大打开文件数 too many open files错误

    遇到一问题,tomcat最近发生几次异常,查看日志,发现一直报 too many open files,熟悉的同学都知道这是用户打开文件数过多导致的, 再用命令ls /proc/20861/fd/ | ...

  9. UNITY Profiler 真机调试

    1,WIFI方式, 2,ADB方式WIFI方式:数据通过WIFI在手机和电脑间传输,且手机和电脑必须在一个网段ADB方式如下:数据通过数据线在手机和电脑间传输.显然ADB方式速度快,无网段限制,比较好 ...

  10. Tomcat运行javaweb项目时出现的一个bug

    Stacktrace:with root cause java.net.ConnectException: Connection refused:........................... ...