[LC] 11. Container With Most Water
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.
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的更多相关文章
- 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 ...
- 如何装最多的水? — leetcode 11. Container With Most Water
炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...
- [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: 找出数组中最大的数 ...
- 《LeetBook》leetcode题解(11):Container With Most Water[M] ——用两个指针在数组内移动
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- LeetCode 11. Container With Most Water (装最多水的容器)
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- 11. Container With Most Water 装水最多的容器
[抄题]: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ...
随机推荐
- Java--HashMap排序
package connection; import java.util.Collections; import java.util.Comparator; import java.util.Hash ...
- [Java-基础] 什么是ORM
ORM简介 ORM:对象关系映射:Object Relational Mapping 用于实现面向对象编程语言里不同类型系统的数据之间的转换 一般的,数据库绝大部分是面向关系的数据库,但是写代码的 ...
- PAT Advanced 1052 Linked List Sorting (25) [链表]
题目 A linked list consists of a series of structures, which are not necessarily adjacent in memory. W ...
- Python重学记录2
这几天学的不多,只是看了一下相关的视频.最近看的部分比较难,装饰器没有搞懂,__slots__和property也不太明白(这两个知识点是在公交车上看的视频,因为1.5倍速度放的视频,看得快,不太明白 ...
- 循环队列--忘记分配空间和如何用tag判断队空队满
#include<iostream> #define maxsize 100 using namespace std; struct CLqueue { int *Q; int front ...
- saturates|meteoric|enclose|marooned|predators|Pioneer community|salinization|condenser|embodied
saturates渗透 meteoric蒸汽 enclose包围 Pioneer community 先锋群落 Climax community顶级群落 cumulative积累 Rebound 回弹 ...
- Java--Runtime.addShutdownHook
转自:http://kim-miao.iteye.com/blog/1662550 一.Runtime.addShutdownHook理解 在看别人的代码时,发现其中有这个方法,便顺便梳理一下. vo ...
- 伯特兰·亚瑟·威廉·罗素[註 1],第三代羅素伯爵(英语:Bertrand Arthur William Russell, 3rd Earl Russell,1872年5月18日-1970年2月2日),OM,FRS,英国哲学家、数学家和逻辑学家,致力于哲学的大众化、普及化。[2] 在數學哲學上採取弗雷格的邏輯主義立場,認為數學可以化約到邏輯,哲學可以像邏輯一樣形式系統化,主張逻辑原子論。[3]
一年假. 1920年7月,罗素申請了一年假; 這被批准了.他花了一年時間在中國和日本講學.对中国学术界有相当影响. 罗素说: 对爱情的渴望,对知识的追求,对人类苦难不可遏制的同情,是支配我一生的单纯 ...
- linux笔记(一)——基本命令和快捷键
linux笔记(一) 1.常用BASH快捷键 编辑命令 快捷键 作用 Ctrl + a 移到命令行首 Ctrl + e 移到命令行尾 Ctrl + xx 在命令行首和光标之间移动 Ctrl + u 从 ...
- PAT甲级——1140.Look-and-say Sequence (20分)
Look-and-say sequence is a sequence of integers as the following: D, D1, D111, D113, D11231, D112213 ...