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. 仿QQ菜单栏:消息,电话菜单

    转载自:http://blog.csdn.net/johnnyz1234/article/details/45919907 在实际项目开发使用Fragment的时候,也碰到一些异常和存在的问题,下面做 ...

  2. sh - 脚本学习 启动/停止/重启/部署jetty crontab

    ===============jettytest.sh ====================== #!/bin/shjettysh_path=/usr/local/jetty/bin/jetty. ...

  3. 在Win10上使用Visual Studio2015的Android模拟器

    在Win10上使用Visual Studio2015的Android模拟器 装上win10后,安装了强大的VS2015,不仅可以开发Windows应用,还可以开发Android和iOS应用,简直神器啊 ...

  4. Web前端新手想提升自身岗位竞争力,需做好这3件事!

    Web前端开发行业的发展前景毋庸置疑,只要是互联网企业,几乎都需要Web前端开发工程师.虽然Web前端入行门槛低,但竞争逐渐激烈,想要取得高薪,就一定要具备强大的实力.那么,在重庆Web前端培训学习中 ...

  5. centos 7.5安装docker-CE 18

    1.查看系统版本 cat /etc/centos-release CentOS Linux release 7.5.1804 (Core) uname -r 3.10.0-862.el7.x86_64 ...

  6. makefile或shell中的一些变量

    总是记不住,作个笔记 $@ 所有目标文件 $< 第一个依赖文件的名称 $? 所有的依赖文件,以空格分开,这些依赖文件的修改日期比目标的创建日期晚 $^ 所有的依赖文件,以空格分开,不包含重复的依 ...

  7. MYSQL ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.10.210' (111) 解决方法

    今天在测试MySQL的连接时候,发现连接不通过,并报错ERROR 2003 (HY000): Can't connect to mysql server on '192.168.10.210' (11 ...

  8. Python的xml模块

    先来一段xml代码 <?xml version="1.0"?> <data> <country name="Liechtenstein&qu ...

  9. python之路——12

    王二学习python的笔记以及记录,如有雷同,那也没事,欢迎交流,wx:wyb199594 复习 1.装饰器 开发原则:开放封闭原则 作用:不改变原函数的调用方式,为函数前后扩展功能 本质:闭包函数 ...

  10. Windows下,python pip安装时ReadTimeoutError解决办法

    一般情况下PIP出现ReadTimeoutError都是因为被GFW给墙了,所以一般遇到这种问题,我们可以选择国内的镜像来解决问题. 在Windows下: C:\Users\Administrator ...