题意:在坐标轴的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 容器的最大装水量的更多相关文章

  1. [LeetCode] 11. Container With Most Water 装最多水的容器

    Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ...

  2. 如何装最多的水? — leetcode 11. Container With Most Water

    炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...

  3. LeetCode 11. Container With Most Water 单调队列

    题意 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai ...

  4. 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 用双指针向中间滑动,较小的高度就作为当前情 ...

  5. Leetcode 11. Container With Most Water(逼近法)

    11. Container With Most Water Medium Given n non-negative integers a1, a2, ..., an , where each repr ...

  6. LeetCode 11. Container With Most Water (装最多水的容器)

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).  ...

  7. [leetcode]11. Container With Most Water存水最多的容器

    Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ...

  8. 【LeetCode每天一题】Container With Most Water(容器中最多的水)

    Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ...

  9. [LeetCode]11. Container With Most Water 盛最多水的容器

    Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ...

随机推荐

  1. Cygwin install apt-cyg

    1. UPDATE CYGWIN First of all you will need to ensure that Cygwin has the necessary binaries require ...

  2. Metasploit和python两种安全工具的学习笔记

    Metasploit是个好东西 主要参考了<Metasploit渗透测试魔鬼训练营>这本书. 一.先用自己的靶机感受一下该工具的强大 linux靶机的ip如图 按照书上写的配置,如图 然后 ...

  3. filter、map、reduce区别

    1.filter filter(function,sequence)-->list,tuple or string 1)       参数func是自定义的过滤函数,在函数func(item)中 ...

  4. mysql安装等操作

    CentOS 6.5系统中安装配置MySQL数据库 卸载掉原有mysql rpm -qa | grep mysql // 这个命令就会查看该操作系统上是否已经安装了mysql数据库 rpm -e my ...

  5. jenkins+maven+Tomcat+shell构建自动化部署

    https://yq.aliyun.com/articles/685931 1.官网下载war包:jenkins本质上就是一个web应用,直接下载jenkins的war包通过tomcat运行即可.ht ...

  6. ELK系列(3) - Elasticsearch修改jvm参数

    方法 Elasticsearch默认会配置1G的JVM堆的初始值和最大值,该jvm参数被配置在/config/jvm.options里: -Xms1g -Xmx1g 如果只是个人开发小项目,可以把参数 ...

  7. PDO中构建事务处理的应用程序

    <meta http-equiv="Content-Type" content="text/html";charse="utf-8" ...

  8. Luogu P4901 排队 fib数列+树状数组+倍增

    这题让我升华..还好只重构了一遍 首先我们发现:$n$较小时,整个队伍的形态 跟 $n$ 比较大时的局部是一样的 所以我们预处理出这个队伍的形态,和每一行每个位置的质因子个数的前缀和,$O(nlogn ...

  9. jacoco-maven-plugin

    <properties> <org.eclipse.persistence.version>2.7.0</org.eclipse.persistence.version& ...

  10. py---------模块和包

    单独导入包 单独导入包名称时不会导入包中所有包含的所有子模块,如 #在与glance同级的test.py中 import glance glance.cmd.manage.main() ''' 执行结 ...