11. Container With Most Water (JAVA)
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)的更多相关文章
- 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 用双指针向中间滑动,较小的高度就作为当前情 ...
- Leetcode 11. Container With Most Water(逼近法)
11. Container With Most Water Medium Given n non-negative integers a1, a2, ..., an , where each repr ...
- LeetCode Array Medium 11. Container With Most Water
Description Given n non-negative integers a1, a2, ..., an , where each represents a point at coordin ...
- 刷题11. Container With Most Water
一.题目说明 11.Container With Most Water,这个题目难度是Medium. 二.我的做法 乍一看,简单啊,两个for循环就可以了,我在本地写的. #include<io ...
- [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: 找出数组中最大的数 ...
- 如何装最多的水? — leetcode 11. Container With Most Water
炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...
- 《LeetBook》leetcode题解(11):Container With Most Water[M] ——用两个指针在数组内移动
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- Java [leetcode 11] Container With Most Water
问题描述: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ...
- LeetCode 11. Container With Most Water (装最多水的容器)
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
随机推荐
- Mad LIbs小游戏
c1=input('请输入摄氏温度;') c2=float(c1)*9/5+32 print('摄氏温度转换成华氏温度是{}'.format(c2)) name1=input('请输入名字:') na ...
- JavaScript 实现打印操作
一.打印当前页面指定元素中的内容 方式一:直接使用window.print(); (1)首先获得元素的html内容(这里建议如果有样式最好是用内联样式的方式) var newstr = documen ...
- PythonStudy——文件操作习题 Document operation exercises
# 1.统计文件数据中字母e出现的次数(不区分大小写)# 文件内容:hello friend, can you speak English!# 结果:4# 分析:将文件内容读出,然后统计读出的字符串中 ...
- 使用fpm 软件包打包
安装 sudo gem install --no-ri --no-rdoc fpm 简单使用 一个 redis的简单demo % ls src/redis-server redis.conf src/ ...
- 更多FMK 的还是看万一的吧
http://www.cnblogs.com/del/category/323943.html 记录一下, 作为目录
- 源码编译、安装net-snmp的方法和遇到的问题
本文参考地址:http://blog.163.com/qiushuhui1989@126/blog/static/270110892014119113421364/ 1. 源码下载 # wget ht ...
- api.js封装请求
1. 传入对象格式如 { a:{ getData:{ url: 'xx/xx/xx', method: 'get', require:['id', 'name'], // 简单检查 必传参数确实则不发 ...
- Xtrabackup2.4.8备份、还原、恢复Mysql5.7.19实操(网络拷贝)
环境:CentOS 6.7 + Mysql 5.7.19 + Xtraback 2.4.8 innobackupex常用参数: --user=USER 指定备份用户,不指定的话为当前系统用户 --p ...
- Django_admin组件
1.Django_admin组件的意义 作者:Eric 微信:loveoracle11g 新建Django项目bms图书管理系统 App为book book/models.py添加表关系 from d ...
- C#操作wps、excel
比如打开表格,如下 object openEt() { ]; Type wpsAppName; string progID = "KET.Application";// " ...