Given n non-negative integers a1a2, ..., an , where each represents a point at coordinate (iai). n vertical lines are drawn such that the two endpoints of line i is at (iai) 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.

The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.

Example:

Input: [1,8,6,2,5,4,8,3,7]
Output: 49
class Solution {
public int maxArea(int[] height) {
int left = 0, right = height.length - 1;
int res = 0;
while (left < right) {
res = Math.max(res, Math.min(height[left], height[right]) * (right - left));
// move the smaller slant
if (height[left] < height[right]) {
left += 1;
} else {
right -= 1;
}
}
return res;
}
}

[LC] 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 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. 如何装最多的水? — leetcode 11. Container With Most Water

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

  6. [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: 找出数组中最大的数 ...

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

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

  8. LeetCode 11. Container With Most Water (装最多水的容器)

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

  9. 11. Container With Most Water 装水最多的容器

    [抄题]: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ...

随机推荐

  1. IOS下的safari不支持localStorage?

    同事在统计日志的时候,想用localStorag去记载一些什么,但是在各大浏览器都运行的良好的基础上,唯独IOS下的safari一直静静无声,没有任何反应.打印localStorage都是Object ...

  2. 使用idea出现的错误

    错误:打开maven项目时出现"程序包 com.sun.org.apache.xpath.internal 不可见 "的错误 这个问题出现的原因是: jdk版本的问题.可能是因为有 ...

  3. Python不区别字符串大小写的列表比较法

    一开始想这样写,结果报了索引错误 后来改正: if user_name.lower() in [names.lower() for names in names]: 我觉得应该是in后面应该跟列表,而 ...

  4. LIINQ TO JS

    记录一下,方便自己查找... 自己在开发前端时,对于处理JSON,觉得真是枯燥.处理数据,基本都要循环. 所以我想着前端也能跟后端一样,有Linq来处理我的JSON对象就好了.上网一搜,找到了JSLI ...

  5. BeanFactory和ApplicationContext的区别(Bean工厂和应用上下文)

    https://blog.csdn.net/qq_20757489/article/details/88543252 https://blog.csdn.net/pythias_/article/de ...

  6. Java之多线程窗口卖票问题(Runnable)

    /** * 例子:创建三个窗口卖票,总票数为100张.使用实现Runnable接口的方式 * 存在线程的安全问题,待解决. */class Window1 implements Runnable{ p ...

  7. Idea创建Spring项目

    环境 win7 + Idea2018 Classpath commons-logging-1.2 + spring-framework-4.1.6.RELEASE Step1 创建工程 File -& ...

  8. python-day2爬虫基础之爬虫基本架构

    今天主要学习了爬虫的基本架构,下边做一下总结: 1.首先要有一个爬虫调度端,来启动爬虫.停止爬虫或者是监视爬虫的运行情况,在爬虫程序中有三个模块,首先是URL管理器来对将要爬取的URL以及爬取过的UR ...

  9. [前端] VUE基础 (5) (过滤器、生命周期、钩子函数)

    一.过滤器 过滤器分为局部过滤器和全局过滤器. 1.局部过滤器 <body> <div id="app"> </div> <script ...

  10. LeetCode——199. 二叉树的右视图

    给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. 示例: 输入: [1,2,3,null,5,null,4] 输出: [1, 3, 4] 解释: 1 < ...