Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.

Note: You may not slant the container and n is at least 2.

Example:

Input: [1,8,6,2,5,4,8,3,7]
Output: 49

class Solution {
public int maxArea(int[] height) {
return calMax(height, 0, height.length-1, 0);
} public int calMax(int[] height, int left, int right, int max){
if(left >= right) return max; if(height[left] > height[right]){
int area = (right-left) * height[right];
return calMax(height, left, right-1, Math.max(area,max));
}
else{
int area = (right-left) * height[left];
return calMax(height, left+1, right, Math.max(area,max));
}
}
}

解题思路:两个指针分别指向左右两个位置,保留高的那个,矮的那个往中间移动。(Greedy:总是寻找高的柱子)

11. Container With Most Water (JAVA)的更多相关文章

  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 Array Medium 11. Container With Most Water

    Description Given n non-negative integers a1, a2, ..., an , where each represents a point at coordin ...

  4. 刷题11. Container With Most Water

    一.题目说明 11.Container With Most Water,这个题目难度是Medium. 二.我的做法 乍一看,简单啊,两个for循环就可以了,我在本地写的. #include<io ...

  5. [leecode]---11.container with most water

    description: Input: [1,8,6,2,5,4,8,3,7]Output: 49 思路1: 从(1,a1)开始向后算面积,需要两层n循环,时间复杂度n2 思路2: 找出数组中最大的数 ...

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

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

  7. 《LeetBook》leetcode题解(11):Container With Most Water[M] ——用两个指针在数组内移动

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  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 (装最多水的容器)

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

随机推荐

  1. EtherCAT 要点

    倍福: 1.倍福的每个从站模块(IO模块)一般都有转换时间而这些时间会导致从站模块输出数值存在延迟,举例:当在15:00开始调用API让主站板卡输出数据,而用示波器实际检测到模块输出数值的时间大约在1 ...

  2. COMBIN14简单应用

    目录 案例1 说明 APDL代码 结果 案例2 说明 APDL代码 结果 案例3 说明 APDL代码 结果 参考网址:http://blog.sina.com.cn/s/blog_65936c2301 ...

  3. mongodb分片balance

    查看balance状态 mongos> sh.getBalancerState()true   通过balance锁查看balance活动 如果state是2,表示balance锁已经被获取 m ...

  4. Qt5 中文乱码问题

    在做gui界面时,使用QTdesigner设计时,控件的名称等输入中文没有问题.为了更加灵活点,直接使用代码进行布局就很有必要了.这样就会出现中文的乱码.为了解决乱码,查看资料说时修改文件保存的编码, ...

  5. Linux之ls

    命令功能: ls是list的简写,列出目录下的内容 命令格式: ls [OPTION]... [FILE]... 命令参数: -a,--all    不忽略以“.”开头的隐藏文件 -A, --almo ...

  6. TF(1): 基础理论

    TensorFlow最初由Google大脑的研究员和工程师开发出来,用于机器学习和神经网络方面的研究,于2015.10宣布开源,在众多深度学习框架中脱颖而出,在Github上获得了最多的Star量.T ...

  7. 用C自撸apache简易模块,搭建图片处理服务器。

    写C是个撸sir /* ** mod_acthumb.c -- Apache sample acthumb module ** [Autogenerated via ``apxs -n acthumb ...

  8. flutter环境配置

    java环境安装 做基于android的原生app,首先需要安装java环境,需要到官网https://www.oracle.com/technetwork/java/javase/downloads ...

  9. 彻底理解js中this的指向,不必硬背

    来自   https://blog.csdn.net/u011088260/article/details/79230661 首先必须要说的是,this的指向在函数定义的时候是确定不了的,只有函数执行 ...

  10. Zookeeper 在Windows下的安装过程及测试

    安装jdk 安装Zookeeper. 在官网http://zookeeper.apache.org/下载zookeeper.我下载的是zookeeper-3.4.6版本. 解压zookeeper-3. ...