题目链接:https://leetcode.com/problems/container-with-most-water/description/

题目大意:给出一串数组(a1, a2, a3, ...an),表示坐标(i, ai),同样表示一条直线是从(i, 0)到(i, ai),从中选出两条直线,计算其容器能蓄水多少,并找出最多的蓄水量。(容器不能倾斜,就是两条直线可以是不同高度,但是蓄水量要按照低高度的那条直线来计算),例子如下:

法一:暴力,对于一条直线,分别遍历左边和右边直线,计算其蓄水量,取最大者。可惜超时了。代码如下:

     public int maxArea(int[] height) {
int res = 0;
for(int i = 0; i < height.length; i++) {
//计算左边直线
for(int j = height.length - 1; j > i; j--) {
if(height[j] >= height[i]) {
res = Math.max(res, (j - i) * height[i]);
break;
}
}
//计算右边直线
for(int j = 0; j < i; j++) {
if(height[j] >= height[i]) {
res = Math.max(res, (i - j) * height[i]);
break;
}
}
}
return res;
}

法二:两指针移动,与42题的两指针移动类似但略有区别,左指针与右指针相比较,记录较大者,若左指针较小,则从左往右移动,若右指针较小,则从右往左移动,如果当前值比较大者大,则计算:(较大者下标-当前值下标)*当前值高度,计算得到最后的结果。代码如下(耗时8ms):

     public int maxArea(int[] height) {
int res = 0, left = 0, right = height.length - 1;
while(left < right) {
//如果左指针<=右指针
if(height[left] <= height[right]) {
while(left < right && height[left] <= height[right]) {
res = Math.max(res, (right - left) * height[left++]);
}
}
//如果左指针>右指针
else {
while(left < right && height[left] > height[right]) {
res = Math.max(res, (right - left) * height[right--]);
}
}
}
return res;
}

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. 刷题11. Container With Most Water

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

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

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

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

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

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

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

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

  8. [LeetCode] 11. Container With Most Water 装最多水的容器

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

  9. leetcode problem 11 Container With Most Water

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

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

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

随机推荐

  1. collection 多态 会自动转型为子类 继承多态需要显示转型

  2. PHP 中数组获取不到元素

    早上看到 SO 上一个有关 PHP 的问题,提问者描述有一个数组,使用 print_r 可以看到索引 key 和相对应的 value 都是存在的,但是访问该元素,不管是使用 array[key] 还是 ...

  3. xpath定位相邻元素方法

    在定位页面元素时,有时候需要根据某个元素特征,去定位其相邻元素/兄弟元素,或者定位其父元素的兄弟元素(或叔伯元素的子元素).这里引入xpath的两个定位方法: preceding-sibling fo ...

  4. Argus UVALive - 3135(优先队列 水题一道)

    有一系列的事件,它每Period秒钟就会产生编号为qNum的事件,你的任务是模拟出前k个事件,如果多个事件同时发生,先处理qNum小的事件 今天再看看数据结构.. #include <iostr ...

  5. CodeForces - 955B(用char会超时。。。)

    #include <bits/stdc++.h> #define mem(a, b) memset(a, b, sizeof(a)) using namespace std; , INF ...

  6. MapReduce(四) 典型编程场景(二)

    一.MapJoin-DistributedCache 应用 1.mapreduce join 介绍 在各种实际业务场景中,按照某个关键字对两份数据进行连接是非常常见的.如果两份数据 都比较小,那么可以 ...

  7. bzoj2314: 士兵的放置(树形DP)

    0表示被父亲控制,1表示被儿子控制,2表示被自己控制.f表示最少士兵数,g表示方案数. 转移贼难写,写了好久之后写不下去了,看了一眼题解,学习了...原来还可以这么搞 比如求f[i][1]的时候,要在 ...

  8. springboot读取自己定义的配置文件的方式以及使用joda_time来处理时间日期

    总的来说呢,有两种方式,一种是原始的方式,即使用PropertiesUtils来读取配置文件. 第二种就是使用springboot的注解的方式来读取配置文件. 1.原始方式处理属性和时间日期: 工具类 ...

  9. Android Monkey 脚本编写与检查内存泄露

    一.Monkey脚本编写 1.Monkey脚本格式 脚本优势: 简单快捷,不需要接触任何工具,只需要一个记事本文件 脚本缺点: 实现坐标.按键等基本操作的相应步骤,顺序脚本无逻辑性 脚本源码: \de ...

  10. WebSocket解释及如何兼容低版本浏览器

    WebSocket类似HTTP 协议,是为了弥补HTTP 协议的缺陷:通信只能由客户端发起,HTTP 协议做不到服务器主动向客户端推送信息. WebSocket 协议在2008年诞生,2011年成为国 ...