当前位置:网站首页>每日一题-盛最多水的容器-0716
每日一题-盛最多水的容器-0716
2022-08-05 05:17:00 【菜鸡程序媛】
题目
给定一个长度为 n 的整数数组 height 。有 n 条垂线,第 i 条线的两个端点是 (i, 0) 和 (i, height[i]) 。
找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。
返回容器可以储存的最大水量。
思路
- 一个下标从左开始走,另一个下标从右开始走。对比两边柱子的高度,因为装水,所以较矮的一边是容器的高度。
- 当前能装的最大体积,就是min(height[left], height[right])*(right-left),用过的一边就需要向前进,再找当前的最大体积。
- 同时需要一个变量,不断的去更新这个容器能装水的最大值。res = Math.max(res, min(height[left], height[right])*(right-left))
代码
public int maxArea(int[] height) {
if(height == null || height.length == 0)
return 0;
int res = 0;
int left = 0, right = height.length - 1;
// 不断的迭代容器可以盛水的最大值
while(left < right){
res = height[left] > height[right] ? Math.max(res, (right - left) * height[right --]) :
Math.max(res, (right - left) * height[left ++]);
}
return res;
}
边栏推荐
猜你喜欢

盘点关于发顶会顶刊论文,你需要知道写作上的这些事情!

深度学习系列(二)优化器 (Optimization)

LeetCode刷题之第530题

HuiFer 带你读懂 BeanFactory getBean 方法

【ts】typescript高阶:模版字面量类型

链表章6道easy总结(leetcode)

C语言入门笔记 —— 分支与循环

单变量线性回归
![[Pytorch study notes] 9. How to evaluate the classification results of the classifier - using confusion matrix, F1-score, ROC curve, PR curve, etc. (taking Softmax binary classification as an example)](/img/ac/884d8aba8b9d363e3b9ae6de33d5a4.png)
[Pytorch study notes] 9. How to evaluate the classification results of the classifier - using confusion matrix, F1-score, ROC curve, PR curve, etc. (taking Softmax binary classification as an example)

LeetCode刷题之第86题
随机推荐
【UiPath2022+C#】UiPath 练习-数据操作
电子产品量产工具(5)- 页面系统实现
6k+ star,面向小白的深度学习代码库!一行代码实现所有Attention机制!
C语言入门笔记 —— 分支与循环
九、响应处理——内容协商底层原理
HuiFer 带你读懂 BeanFactory getBean 方法
二、自动配置之底层注解
C语言—扫雷的实现
「实用」运维新手一定不能错过的17 个技巧
八、响应处理——ReturnValueHandler匹配返回值处理器并处理返回值原理解析
[Pytorch study notes] 10. How to quickly create your own Dataset dataset object (inherit the Dataset class and override the corresponding method)
网管日记:故障网络交换机快速替换方法
WCH系列芯片CoreMark跑分
CVPR2021 - Inception Convolution with Efficient Dilation Search
《基于机器视觉的输电线路交叉点在线测量方法及技术方案》论文笔记
CVPR最佳论文得主清华黄高团队提出首篇动态网络综述
【UiPath2022+C#】UiPath If条件语句
【UiPath2022+C#】UiPath Switch
单变量线性回归
The University of Göttingen proposed CLIPSeg, a model that can perform three segmentation tasks at the same time