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. pycharm 在线激活

    1.在pycharm过期页面选择 enter License 2.激活界面的License server输入:http://idea.liyang.io 然后点击激活  (确保电脑能上网,亲测有效)

  2. PythonStudy——列表的常用操作 List of common operations

    # 1.索引取值: 列表名[index] s1 = [1, 3, 2] print(s1[0]) print(s1[-1]) # 2.列表运算: 得到的是新list s2 = [1, 2, 3] pr ...

  3. kafka-producer配置

    kafka-producer版本对比 Kafka的producer的API根据版本的不同分为kafka0.8.1.X之前的 kafka.javaapi.producer.Producer.以及之后版本 ...

  4. java实现导入excel功能

    实现功能: 1.Excel模板下载 2.导入excel 一.jsp效果和代码 <form id="uploadForm" target="frameFile&quo ...

  5. zabbix之 自定义内存使用率监控报警

    配置zabbix当内存剩余不足15%的时候触发报警   zabbix默认的剩余内存报警:Average Lack of available memory on server {HOST.NAME}{T ...

  6. Python 回调函数

    什么是回调函数? 回调函数就是一个通过函数指针调用的函数.如果你把函数的指针(地址)作为参数传递给另一个函数,当这个指针被用来调用其所指向的函数时,我们就说这是回调函数: 这是官方的解释,理解上有点费 ...

  7. Zuul 跨域

    JS访问会出现跨域问题的解决, 一.对单个接口,处理跨域,只需要在被调用的类或或方法增加注解 CoossOrigin 如下设置 allowCredenticals=true,表示运行Cookie跨域 ...

  8. QQ群成员发言次数统计(正则表达式版)

    1.先将QQ群的消息记录以.txt文件格式导出来,保存路径及名称自己定义(在本文我导出到Y盘,命名为test.txt) 2.程序如下: data statistics1; if _n_=1 then ...

  9. MySQL Point in Time Recovery the Right Way

    In this blog, I’ll look at how to do MySQL point in time recovery (PITR) correctly. Sometimes we nee ...

  10. IIC时序详解

    Verilog IIC通信实验笔记 Write by Gianttank 我实验的是 AT24C08的单字节读,单字节写,页读和页写,在高于3.3V系统中他的通信速率最高400KHZ的,我实验里用的是 ...