package leetcode.day_01_30;

/**
* 给你 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 <= 105
* 0 <= height[i] <= 104
*
* @author soberw
* @Classname MaxArea0011
* @Description
* @Date 2022-01-30 21:16
*/
public class MaxArea0011 {
// 超时
// public int maxArea(int[] height) {
// int maxArea = 0;
// for (int i = 0; i < height.length - 1; i++) {
// for (int j = i + 1; j < height.length; j++) {
// maxArea = Math.max(Math.min(height[i], height[j]) * (j - i), maxArea);
// }
// }
// return maxArea;
// }
public int maxArea(int[] height) {
int maxArea = 0;
for (int i = 0, j = height.length - 1; i < j; ) {
maxArea = Math.max(Math.min(height[i], height[j]) * (j - i), maxArea);
if (height[i] < height[j]){
i++;
}else {
j--;
}
}
return maxArea;
}
}

LeetCode随缘刷题之盛最多水的容器的更多相关文章

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

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

  2. LeetCode第十一题-可以装最多水的容器

    Container With Most Water 问题简介:通过一个给定数组,找出最大的矩形面积 问题详解:给定一个数组,包含n个非负整数a1,a2,…,an,其中每个表示坐标(i,ai)处的点,绘 ...

  3. C#LeetCode刷题之#11-盛最多水的容器(Container With Most Water)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3615 访问. 给定 n 个非负整数 a1,a2,...,an,每 ...

  4. LeetCode(11):盛最多水的容器

    Medium! 题目描述: 给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .画 n 条垂直线,使得垂直线 i 的两个端点分别为 (i, ai) 和 (i, ...

  5. leecode第十一题(盛最多水的容器)

    class Solution { public: int maxArea(vector<int>& height) { int len=height.size();//错过,少了i ...

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

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

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

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

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

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

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

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

随机推荐

  1. Linux中ssh登陆慢的两种原因

    useDNS配置导致登陆慢 如果ssh server的配置文件(通常是 /etc/ssh/sshd_config )中设置 useDNS yes ,可能会导致 ssh 登陆卡住几十秒.将该配置项设为 ...

  2. 建造者模式(python)

    建造者模式将复杂对象的构建与其表示分离.建造者模式主要有两个参与者:建造者(builder)和指挥者(director) 来自为知笔记(Wiz)

  3. ES6扩展运算符(...)

    来自为知笔记(Wiz)

  4. Python实战案例系列(一)

    本节目录 烟草扫码数据统计 奖学金统计 实战一.烟草扫码数据统计 1. 需求分析 根据扫码信息在数据库文件中匹配相应规格详细信息,并进行个数统计 条码库.xls 扫码.xlsx 一个条码对应多个规格名 ...

  5. linux获取 GPG 密钥失败

    实质性问题就是自己系统没有yum的GPG密钥 查看自己系统版本 cat /etc/issue 登陆mirrors.163.com 找到自己系统对应的密钥  RPM-GPG-KEY-CentOS-3   ...

  6. uniapp页面跳转传递参数过长

    传参 url:'./photo_detail?item='+encodeURIComponent(JSON.stringify(obj)) 取参 const item = JSON.parse(dec ...

  7. 手把手教你分析解决MySQL死锁问题

    在生产环境中如果出现MySQL死锁问题该如何排查和解决呢,本文将模拟真实死锁场景进行排查,最后总结下实际开发中如何尽量避免死锁发生. 一.准备好相关数据和环境 当前自己的数据版本是8.0.22 mys ...

  8. 个人作业2-Java代码实现数据检索并实现可视化

    1.bean实体层 package bean; public class Bean { private String title; private String Abstract; private S ...

  9. 【代码分享】用redis+lua实现多个集合取交集并过滤,类似于: select key from set2 where key in (select key from set1) and value>=xxx

    redis中的zset结构可以看成一个个包含数值的集合,或者认为是一个关系数据库中用列存储方式存储的一列. 需求 假设我有这样一个数据筛选需求,用SQL表示为: select key from set ...

  10. NAO机器人开发环境配置

    python python2.7用于NAO开发 https://www.python.org/downloads/release/python-2718/ python3.6用于其他程序测试. htt ...