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). ...
随机推荐
- [CentOS7] iconv编程转换
声明:本文主要总结自:鸟哥的Linux私房菜-第九章.vim 程式編輯器,如有侵权,请通知博主 (-- 源自鸟哥的私房菜) 首先用Notepad++新建个文件来做这个实验,在Windows平台下新建个 ...
- 数据绑定—Source(绑定到静态类的静态属性)
<UserControl x:Class="绑定.绑定Source" xmlns="http://schemas.microsoft.com/winfx/2006/ ...
- 解读人:朱月琴,Hippocampal proteomic alteration in triple transgenic mouse model of Alzheimer’s disease and implication of PINK 1 regulation in donepezil treatment
文章中文名:阿尔茨海默病三联转基因小鼠模型的海马蛋白质组学改变及Donepezil治疗中PINK 1调节的意义 发表时间:(2019年4月) IF:3.95 单位:澳门大学,威斯康星大学,暨南大学,广 ...
- 浏览器Quirksmode(怪异模式)与标准模式
由于历史的原因,各个浏览器在对页面的渲染上存在差异,甚至同一浏览器在不同版本中,对页面的渲染也不同.在W3C标准出台以前,浏览器在对页面的渲染上没有统一规范,产生了差异(Quirks mode或者称为 ...
- Java学习笔记——Map接口
Map接口 Map接口 Map接口中键和值一一映射. 可以通过键来获取值. 异常 NoSuchElementException:访问的值不存在 ClassCastException:对象类型错误 Un ...
- iOS拼图
#import "ViewController.h" @interface ViewController () @end @implementation ViewContro ...
- POJ1321-棋盘问题
题目链接:点击打开链接 Description 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和 ...
- docker镜像的分层结构三
docker的镜像分层 docker里的镜像绝大部分都是在别的镜像的基础上去进行创建的,也就是使用镜像的分层结构. 实验 比如说使用dockerfile去创建一个最简单的hello镜像.创建好对应的d ...
- onbeforeunload与onunload事件
Onunload,onbeforeunload都是在刷新或关闭时调用,可以在<script>脚本中通过 window.onunload来指定或者在<body>里指定.区别在于o ...
- py---------网络编程
一.软件开发架构 我们了解的涉及到两个程序之间通讯的应用大致可以分为两种: 第一种是应用类:qq.微信.网盘.优酷这一类是属于需要安装的桌面应用 第二种是web类:比如百度.知乎.博客园等使用浏览器访 ...