Problem: 已知n条垂直于x轴的线段,选择其中两条,使得该线段和x轴能够存储最大量的水。
 
1、首先如何计算存储水量 横坐标之差乘以最低高度
2、遍历所有情况则需要n*(n-1)/2,时间复杂度为o(n^2)
3、根据木桶原理,容水量由最短木板决定,因此在遍历时,按照最短木板原理进行对i,j线段的选择
 
参考代码:
package leetcode_50;

/***
*
* @author pengfei_zheng
* 最大容水量问题
*/
public class Solution11 {
public int maxArea(int[] height) {
int left = 0, right = height.length - 1;
int maxArea = 0;//初始化最大容水量为0 while (left < right) {//遍历
maxArea = Math.max(maxArea, Math.min(height[left], height[right])
* (right - left));//更新最大容水量
if (height[left] < height[right])//当左边高度低于右边高度时,left++
left++;
else
right--;
}
return maxArea;
}
}

LeetCode 11 Container With Most Water(分支​判断问题)的更多相关文章

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

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

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

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

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

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

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

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

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

  6. C#解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, ...

  8. Java [leetcode 11] Container With Most Water

    问题描述: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ...

  9. [LeetCode] 11. Container With Most Water My Submissions Question 解题思路

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

随机推荐

  1. ORACLE 几个我忍了他很多年的问题

    ① DataGuard环境,主库创建的临时文件,不在备库上创建 ② 创建11G R2 Rac环境时,在RHEL 6.0及以后版本上,最后执行root.sh命令时,以及重新启动OS后启动ohasd,都需 ...

  2. CentOS 7 打开关闭FirewallD防火墙端口命令

    CentOS 7 使用firewalld代替了原来的iptables,使用方法如下: >>>关闭防火墙 systemctl stop firewalld.service        ...

  3. SVN中图标符号的含义

    黄色感叹号(有冲突): 这是有冲突了,冲突就是说你对某个文件进行了修改,别人也对这个文件进行了修改,别人抢在你提交之前先提交了,这时你再提交就会被提示发生冲突,而不允许你提交,防止你的提交覆盖了别人的 ...

  4. Xcode密钥没有备份或者证书过期,出现Valid Signing错误

    密钥没有备份 或者证书过期,和Xcode 4.4中的证书,出现  Valid Signing 错误时   1.生成私有证书,打开钥匙串,钥匙串访问 – 证书助理 – 从证书颁发机构请求证书…,填入iD ...

  5. centos6.5 手动安装gcc

    gcc版本:gcc-4.4.7 rpm -Uvh  mpfr-2.4.1-6.el6.x86_64.rpm rpm -Uvh  cpp-4.4.7-16.el6.x86_64.rpmrpm -Uvh  ...

  6. 在ListView中使用GridView, Style这样写:

    参考:http://msdn.microsoft.com/zh-cn/library/vstudio/ms788747.aspx 需求,自定义ListView中的滚动条 这岂不是很简单?刷刷刷写完了, ...

  7. mysql中如何查看某个日期所在的周一是几号?某个日期所在的一周开始时间是几号?

    需求描述: 在编写SQL的时候,有这么个需求,就是要查出来某个日期所在的周一是几号,进行了测试,在此进行记录下. 测试过程: 议题:查看某个日期所在的周一是几号 分析:如果某个日期是周一,那么加上整数 ...

  8. 【2018年12月14日】A股最便宜的股票

    新钢股份(SH600782) - 当前便宜指数:193.12 - 滚动扣非市盈率PE:2.91 - 动态市净率PB:0.96 - 动态年化股息收益率:1.75% - 新钢股份(SH600782)的历史 ...

  9. Maven -- 发布jar包至远程仓库

    啦啦啦

  10. 【安全开发】PHP安全编码规范

    申明:本文非笔者原创,原文转载自:https://github.com/SecurityPaper/SecurityPaper-web/blob/master/_posts/2.SDL%E8%A7%8 ...