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. c# 连接mysql配置config,不用装net connector

    <system.data> <DbProviderFactories> <remove invariant="MySql.Data.MySqlClient&qu ...

  2. Unity3d 动态加载材质方法

    Texture img = (Texture)Resources.Load("LedPicture"); GameObject.Find("Led").rend ...

  3. jQuery使用scrollTop获取div标签的滚动条已滚动高度(jQuery版本1.6+时,用prop()方法代替attr()方法)

    $("#content").append('<div>' + data.msg + '</div>'+.'<br>');$('#content' ...

  4. 【转】maven同时使用maven-surefire-report-plugin和maven-surefire-plugin默认将执行两次test

    https://issues.apache.org/jira/browse/SUREFIRE-753 Here the pom.xml snippet how i configured the rep ...

  5. 在selenium中使用css选择器进行元素定位

    Sizzle Css3还提供一些直接选取form表单元素的伪类 :input: Finds all input elements (includes textareas, selects, and b ...

  6. perforce变量配置与使用

    linux Perforce 使用 1.配置环境变量: 下载  perforce    放到/usr/bin/下 export P4PORT=192.168.4.88:1666 #P4所在的主机exp ...

  7. 如何通过Node.js启动cesium

    设置一个Web服务器通过Node.js是很容易的,只需要3个步骤: (1)从安装Node.js网站,你可以使用默认安装设置. (2)打开命令行,然后进入Cesium的根目录,通过npm install ...

  8. python通过标准输入读取内容,读取键盘输入的内容?接收用户输入?

    需求说明: 在交互式脚本中,需要用户手动输入内容,并对内容进行处理.在这里记录下通过 python的内置函数input()读取标注输入的内容.默认的标准输入是键盘. 操作过程: 1.通过input() ...

  9. Java实现经理与员工的差异

    对于在同一家公司工作的经历和员工而言,两者是有很多共同点的.例如,每个月都要发工资,但是经理在完成目标任务后,还会获得奖金.此时,利用员工类来编写经理类就会少写很多代码,利用继承技术可以让经理类使用员 ...

  10. hdu 2348 Turn the corner(三分&amp;&amp;几何)(中等)

    Turn the corner Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...