当前位置:网站首页>每日一题-盛最多水的容器-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;
}
边栏推荐
猜你喜欢
Thread handler handle IntentServvice handlerThread
CVPR 2022 | 70% memory savings, 2x faster training
CVPR best paper winner Huang Gao's team from Tsinghua University presented the first dynamic network review
LeetCode刷题之第61题
SQL (2) - join window function view
网络通信及相关函数介绍
The University of Göttingen proposed CLIPSeg, a model that can perform three segmentation tasks at the same time
CVPR2020 - 自校准卷积
【UiPath2022+C#】UiPath 练习-数据操作
PID详解
随机推荐
网络信息安全运营方法论 (下)
LeetCode刷题之第746题
(C语言)strlen、strcpy、strcat、strcmp、strstr函数的模拟实现
OSPF网络类型
【Promise高级用法】实现并行和串行API
网工必用神器:网络排查工具MTR
每日一题-字典
[Database and SQL study notes] 8. Views in SQL
[Pytorch study notes] 10. How to quickly create your own Dataset dataset object (inherit the Dataset class and override the corresponding method)
如何组织一场安全、可靠、高效的网络实战攻防演习?
It turns out that the MAE proposed by He Yuming is still a kind of data enhancement
leetCode刷题之第31题
CVPR最佳论文得主清华黄高团队提出首篇动态网络综述
链表章6道easy总结(leetcode)
You should write like this
C语言程序死循环问题解析——变量被修改
[Intensive reading of the paper] R-CNN's Bounding box regression problem is detailed
LeetCode刷题之第23题
一个小时教你如何掌握ts基础
浅谈遇到的小问题