LeetCode随缘刷题之盛最多水的容器
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随缘刷题之盛最多水的容器的更多相关文章
- leetcode刷题11. 盛最多水的容器
做题连接https://leetcode-cn.com/problems/container-with-most-water/submissions/ 本题分为两种方法: 暴力法: int maxAr ...
- LeetCode第十一题-可以装最多水的容器
Container With Most Water 问题简介:通过一个给定数组,找出最大的矩形面积 问题详解:给定一个数组,包含n个非负整数a1,a2,…,an,其中每个表示坐标(i,ai)处的点,绘 ...
- C#LeetCode刷题之#11-盛最多水的容器(Container With Most Water)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3615 访问. 给定 n 个非负整数 a1,a2,...,an,每 ...
- LeetCode(11):盛最多水的容器
Medium! 题目描述: 给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .画 n 条垂直线,使得垂直线 i 的两个端点分别为 (i, ai) 和 (i, ...
- leecode第十一题(盛最多水的容器)
class Solution { public: int maxArea(vector<int>& height) { int len=height.size();//错过,少了i ...
- LeetCode:盛最多水的容器【11】
LeetCode:盛最多水的容器[11] 题目描述 给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 ...
- Java实现 LeetCode 11 盛最多水的容器
11. 盛最多水的容器 给定 n 个非负整数 a1,a2,-,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0) ...
- 力扣Leetcode 11. 盛最多水的容器
盛最多水的容器 给你 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找 ...
- LeetCode---11. 盛最多水的容器(Java)
11. 盛最多水的容器 题目地址:https://leetcode-cn.com/problems/container-with-most-water/ 给你 n 个非负整数 a1,a2,...,an ...
随机推荐
- Python常用功能函数系列总结(二)
本节目录 常用函数一:sel文件转换 常用函数二:refwork文件转换 常用函数三:xml文档解析 常用函数四:文本分词 常用函数一:sel文件转换 sel是种特殊的文件格式,具体应用场景的话可以 ...
- Maven+ajax+SSM实现删除
转载自:https://www.cnblogs.com/kebibuluan/p/9020381.html 3.尚硅谷_SSM高级整合_使用ajax操作实现删除的功能 点击删除的时候,要删除联系人,这 ...
- kafka学习笔记(七)kafka的状态机模块
概述 这一篇随笔介绍kafka的状态机模块,Kafka 源码中有很多状态机和管理器,比如之前我们学过的 Controller 通道管理器 ControllerChannelManager.处理 Con ...
- Visaul Studio 2015 MFC控件使用之--按钮(Button)
在MFC开发当中,比较常用的控件之一便是Button控件了,该控件的除了可以通过点击产生的开关量当作开关来使用,还可以设置其颜色变化当作显示灯,按钮控件的使用相对来比较简单. 打开工程解决方案的资源视 ...
- kubernetes运行应用2之DaemonSet详解
kubernetes运行应用1之Deployment详解 查看daemonset 如下,k8s自身的 DaemonSet kube-flannel-ds和kube-proxy分别负责在每个结点上运 ...
- Spring Boot Starter 和 ABP Module
Spring Boot 和 ABP 都是模块化的系统,分别是Java 和.NET 可以对比的框架.模块系统是就像乐高玩具一样,一块一块零散积木堆积起一个精彩的世界.每种积木的形状各不相同,功能各不相同 ...
- golang中的tcp编程
1. tcp server package main import ( "bufio" "fmt" "net" ) func main() ...
- [WAF攻防]从WAF攻防角度重看sql注入
从WAF攻防角度重看sql注入 攻防都是在对抗中逐步提升的,所以如果想攻,且攻得明白,就必须对防有深刻的了解 sql注入的大体流程 Fuzz测试找到注入点 对注入点进行过滤检测,及WAF绕过 构建pa ...
- find -or 用法
find /opt/IBM/WebSphere85/ -name *loggeter* - or -name *loggetter* | xargs rm -rf
- python封装函数到模块
导入整个模块: import 模块名 导入特定函数: from module_name import function_name 通过逗号可以分割函数名,如果需要导入多个则 from a import ...