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). ...
随机推荐
- 安装mysql8.0.11及修改root密码、连接navicat for mysql的思路详解
1.1. 下载: 官网下载zip包,我下载的是64位的: 下载地址:https://dev.mysql.com/downloads/mysql/ 下载zip的包: 下载后解压:(解压在哪个盘都可以的) ...
- Docker 企业级镜像仓库 Harbor 的搭建与维护
目录 一.什么是 Harbor 二.Harbor 安装 2.1.Harbor 安装环境 2.2.Harbor安装 2.3 配置HTTPS 三.Harbor 的使用 3.1.登录Harbor并使用 3. ...
- ProtoBuf练习(五)
表类型 protobuf语言的maps字段类型相当于C++语言的std::map类型 工程目录结构 $ ls proto/ sample_maps.proto proto文件 $ cat proto/ ...
- servlet之doPost()、doGet()
1.doGet和doPost方法的具体应用?即在什么时候程序调用doGet方法,什么时候程序执行doPost方法? HttpServlet是从GenericServlet继承而来,因此HttpServ ...
- CF17E Palisection(回文自动机)
题意翻译 给定一个长度为n的小写字母串.问你有多少对相交的回文子 串(包含也算相交) . 输入格式 第一行是字符串长度n(1<=n<=2*10^6),第二行字符串 输出格式 相交的回文子串 ...
- IDEA的database插件无法链接mysql的解决办法(08001错误)
1.问题 首先先说问题,用navicat链接数据库正常,mysql控制台操作正常,但是用IDEA的数据库插件链接一直报 08001 错误,具体见下图: 错误:Connection to eshop@l ...
- [USACO09OCT]热浪Heat Wave Dijkstra
题目描述 The good folks in Texas are having a heatwave this summer. Their Texas Longhorn cows make for g ...
- JMeter - 连续性能测试 - JMeter + ANT + Jenkins集成 - 第2部分
目标: 创建包含性能测试流程的持续交付管道,以尽早检测任何与性能相关的问题. 通常,全面的性能测试将在分段/预生产环境中完成,该环境可能与您的生产环境相同.在完成QA功能/回归验证后,将代码推送到分段 ...
- PIL图片格式转换
PIL格式转换 原图: #!/usr/local/bin/python # -*- coding: utf8 -*- from PIL import Image, ImageFilter import ...
- [Leetcode]011. Container With Most Water
public class Solution { public int maxArea(int[] height) { int left = 0, right = height.length - 1; ...