LeetCode OJ Container With Most Water 容器的最大装水量
题意:在坐标轴的x轴上的0,1,2,3,4、、、、n处有n+1块木板,长度不一,任两块加上x轴即可构成一个容器,其装水面积为两板的间距与较短板长之积,以vector容器给出一系列值,分别代表在0,1,2,3,4、、、vector.size()-1共size个短板,是连续的,不排除有板长为0的可能性,但是至少有两块板。根据所给板长,求任两板与x轴所能构成的最大面积并返回。
代码:
class Solution {
public:
int maxArea(vector<int> &height) {
int maxArea = , area;
int left = , right = height.size() - ;
while (left < right) {
area = (right - left) * (height[left] < height[right] ? height[left] : height[right]);
maxArea = (maxArea > area) ? maxArea : area;
if (height[left] < height[right])
++left;
else
--right;
}
return maxArea;
}
};
Container With Most Water
思路:
i, j分别从头尾开始遍历,面积 area = min(height[j], height[i]) * (j-i),当height[i] < height[j]时,此时面积 area = height[i] * (j-i); 由于i是短板,不管跟其右边的哪块板组合,它能达到的最大面积取决于 j-i(即两板之间的距离),而此时的j-i的值是最大的,因此,此面积即为以i为左边界、以j为右边界的当前最大面积,然后++i(即将短的边界往中移,寻找更高的且能使面积更大的板,更新area,继续++i,直到i板比j板高,才开始移动右边界j板);同理得j的变化。因为对于i, j,总有一个是短板(相等则随机取一个即可),每次是短板的就发生变化,因此覆盖了所有情况。
从式子area = height[i] * (j-i)开始分析更容易理解,当前面积已经是area,若要使其更大,必须改变i或j的值(式子中也就两个自变量),改变了j的值,(j-i)只会更小,而height[i]没变,那么area就变小。如果改变了i的值,(j-i)也会更小,但是height[i]会变化了,只要height[i]的值比构成当前最大面积的短板更大就有可能使area变大。现在问题转化成寻找一块比i更长的板以试图扩大area,那么从i+1开始往右逐个遍历,首先寻找一个更大的板B,判断height[B]*(j-B)是否大于area,若是,i=B,若否,继续遍历。在每次寻找到一块更高的板时,需要判断是否需要换方向遍历,因为从一开始的假设就是i为短板,若j为短板,则需要将j往左遍历了,理同i(一样是用式子来分析)。
每个板最多才遍历一次,所以算法复杂度O(n),其他算法不一定可行,会超时。若不考虑超时,可以穷举任两板来构成最大面积,当然还可以进行剪枝,比如:当以第i块板来与其左边所有板配合,想要找一个更大的面积,必须板间距必须大于当前area/height[i],可以剪掉距离i板为area/height[i]以下的一些板的计算。
LeetCode OJ Container With Most Water 容器的最大装水量的更多相关文章
- [LeetCode] 11. Container With Most Water 装最多水的容器
Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ...
- 如何装最多的水? — leetcode 11. Container With Most Water
炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...
- LeetCode 11. Container With Most Water 单调队列
题意 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai ...
- leetcode 11. Container With Most Water 、42. Trapping Rain Water 、238. Product of Array Except Self 、407. Trapping Rain Water II
11. Container With Most Water https://www.cnblogs.com/grandyang/p/4455109.html 用双指针向中间滑动,较小的高度就作为当前情 ...
- Leetcode 11. Container With Most Water(逼近法)
11. Container With Most Water Medium Given n non-negative integers a1, a2, ..., an , where each repr ...
- LeetCode 11. Container With Most Water (装最多水的容器)
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- [leetcode]11. Container With Most Water存水最多的容器
Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ...
- 【LeetCode每天一题】Container With Most Water(容器中最多的水)
Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ...
- [LeetCode]11. Container With Most Water 盛最多水的容器
Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ...
随机推荐
- js常用util
/** 日期格式化 */Date.prototype.Format = function(format) { var o = { "M+" : this.getMonth() + ...
- C#中的运算符和表达式
说起C#运算符和表达式,小伙伴们肯定以为很简单,其实要用好表达式,不是一件容易的事.一个好的表达式可以让你做事半功倍的效果,比如三元表达式,可以让你少写N多个if和case语句. 表达式 由 操作数( ...
- 一切从这里起始(左耳听风 ARTS 6号小组 week 1)
ARTS 具体要求: 1.每周至少做一个 leetcode 的算法题2.阅读并点评至少一篇英文技术文章3.学习至少一个技术技巧4.分享一篇有观点和思考的技术文章 1.Algorithm Two Sum ...
- Angular输入框内按下回车会触发其它button的点击事件的解决方法
方法:给button按钮添加type=“botton”属性
- MySQL的复制:MySQL系列之十三
一.MySQL复制相关概念 主从复制:主节点将数据同步到多个从节点 级联复制:主节点将数据同步到一个从节点,其他的从节点在向从节点复制数据 同步复制:将数据从主节点全部同步到从节点时才返回给用户的复制 ...
- 关于unique去重
嗯.... unique这个东西也是一个冷门知识..... 但是在有时候它还是比较好用的东西... 下面就在详细代码中看unique是如何实际应用的....它主要是用于数组去重 #include< ...
- 获取Spring应用环境上下文bean
import org.springframework.beans.BeansException; import org.springframework.beans.factory.NoSuchBean ...
- Bios启动模式:Legacy/UEFI
1.1 UEFI Bios启动模式 UEFI Bios支持两种启动模式:Legacy+UEFI启动模式和UEFI启动模式,其中Legacy+UEFI启动模指的是UEFI和传统BIOS共存模式,可以兼容 ...
- scau 8616 汽车拉力比赛
上次我们过了二分图的最佳匹配,现在我们看一道题目,经典的二分图的最佳匹配题目 8616 汽车拉力比赛 时间限制:500MS 内存限制:1000K提交次数:71 通过次数:24 题型: 编 ...
- Spark 概述
Spark 是什么? ● 官方文档解释:Apache Spark is a fast and general engine for large-scale data processing. 通俗的理解 ...