题目描述

给你 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0) 。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。

说明:你不能倾斜容器。

示例 1:

输入:[1,8,6,2,5,4,8,3,7]

输出:49

解释:图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7]。在此情况下,容器能够容纳水(表示为蓝色部分)的最大值为 49。

示例 2:

输入:height = [1,1]

输出:1

示例 3:

输入:height = [4,3,2,1,4]

输出:16

示例 4:

输入:height = [1,2,1]

输出:2

提示:

  • n = height.length
  • 2 <= n <= 3 * 104
  • 0 <= height[i] <= 3 * 104

解法

方法一:暴力求解,不展示了

方法二:双指针

首尾两个指针,一次移动一个指针,移动边长更短的那个指针,直到指针重合

时间复杂度为O(n) 空间复杂度为O(1)

class Solution {
public int maxArea(int[] height) {
int pi = 0, pj = height.length - 1;
int mx = 0;
while (pi != pj) {
mx = Math.max((pj-pi)*Math.min(height[pi], height[pj]), mx);
if (height[pi] >= height[pj]) {
pj--;
}
else {
pi++;
}
}
return mx;
}
}
class Solution {
public int maxArea(int[] height) {
int pi = 0, pj = height.length - 1;
int mx = 0, temp = 0;
while (pi != pj) {
temp = height[pi] >= height[pj] ? height[pj--] * (pj - pi + 1) : height[pi++] * (pj - pi + 1);
mx = Math.max(temp, mx);
}
return mx;
}
}

不写if判断语句貌似要快一些,不知道是不是不需要分支预测。但是第二版也是要分支预测吧!

leetcode 刷题(数组篇)11题 盛最多水的容器(双指针)的更多相关文章

  1. LeetCode 11 - 盛最多水的容器 - [双指针暴力]

    题目链接:https://leetcode-cn.com/problems/container-with-most-water/description/ 给定 n 个非负整数 $a_1,a_2,\cd ...

  2. Leetcode(11)-盛最多水的容器

    给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .画 n 条垂直线,使得垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的两条线,使得 ...

  3. LeetCode:盛最多水的容器【11】

    LeetCode:盛最多水的容器[11] 题目描述 给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为  ...

  4. Java实现 LeetCode 11 盛最多水的容器

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

  5. 力扣Leetcode 11. 盛最多水的容器

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

  6. LeetCode---11. 盛最多水的容器(Java)

    11. 盛最多水的容器 题目地址:https://leetcode-cn.com/problems/container-with-most-water/ 给你 n 个非负整数 a1,a2,...,an ...

  7. 【LeetCode】11. Container With Most Water 盛最多水的容器

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:盛水,容器,题解,leetcode, 力扣,python ...

  8. leetcode刷题11. 盛最多水的容器

    做题连接https://leetcode-cn.com/problems/container-with-most-water/submissions/ 本题分为两种方法: 暴力法: int maxAr ...

  9. LeetCode随缘刷题之盛最多水的容器

    package leetcode.day_01_30; /** * 给你 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点(i,ai) .在坐标内画 n 条垂直线,垂直线 i的两个端 ...

随机推荐

  1. 「NGK每日快讯」12.23日NGK第50期官方快讯!

  2. NGK Global伦敦路演:“区块链+能源”必将推动世界性能源革命

    随着区块链技术的发展和应用的不断完善深入,市场的热情也开始活跃高涨,在万众期待下,NGK Global在英国伦敦的路演于7月25日圆满举办. 此次伦敦路演会议中众多行业精英,各社区代表.星盟投资公司资 ...

  3. java数据类型(进阶篇)

    public class note03 { public static void main(String[] args) { //数据类型拓展 //1.整数拓展 //进制: 二进制0b 十进制 八进制 ...

  4. NDK android Error:Expected caller to ensure valid ABI: MIPS

    android studio 安装NDK之后,报错 Error:Expected caller to ensure valid ABI: MIPS 环境: android studio 2.3 gra ...

  5. 微服务学习.net5+consul

    趁着刚过完年,还没有开始做业务的时候,学习下consul 概念自己去官网看,这里只讲下具体实现 官网下载https://www.consul.io/downloads 我下载的是Windows版本 启 ...

  6. cartographer 调参(1)-lua文件配置参考文档

    cartographer 调参(1)-lua文件配置参考文档 https://blog.csdn.net/SimileciWH/article/details/84861718 Lua configu ...

  7. js的基本数据类型与引用数据类型

    基本数据类型与引用数据类型 基本数据类型有五种 /* 基本数据类型有: - String - Number - Boolean - Null ** typeof null === 'object' 这 ...

  8. 测试成长记录:python调adb无法获取设备信息bug记录

    背景介绍: 一直在负责公司Android自动化的编写工作,采用的是uiautomator2,需要获取设备id来连接设备,就是 adb devices 问题描述: 之前一直用 subprocess.ch ...

  9. #progma pack(x)说明

    1.字节对齐(内存相关) 现代计算机中内存空间都是按照byte划分的,从理论上讲似乎对任何类型的变量的访问可以从任何地址开始,但实际情况是在访问特定变量的时候经常在特定的内存地址访问,这就需要各类型数 ...

  10. 用java输出"Hello,World!"

    Hello,World! 一般步骤 随便新建一个文件夹,存放代码 新建一个java文件 新建.txt文档 文件后缀名改为.java Hello.java [注意]系统可能没有显示后缀名,我们需要手动打 ...