[leetcode]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
题意:
给定一堆高矮不一的平行的墙,选择其中两堵作为两壁,问最多能存多少水。
Solution1:
Two Pointers: set pointer left at index 0; set pointer right at index len - 1
We calculate area base on width (gap of left and right index ) and height(smaller height matters because of "Short-board effect" )
In order to find max area, we move the pointer whichever representing the smaller height
code:
/*
Time complexity : O(n). We traverse the whole give array
Space complexity : O(1). We only used constant extra space.
*/ class Solution {
public int maxArea(int[] height) {
int left = 0;
int right = height.length - 1;
int area = Integer.MIN_VALUE;
while(left < right){
area = Math.max(area, Math.min(height[left], height[right]) * (right - left));
if(height[left] < height[right]){
left ++;
}else{
right --;
}
}
return area;
}
}
[leetcode]11. Container With Most Water存水最多的容器的更多相关文章
- LeetCode 11. Container With Most Water (装最多水的容器)
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- Leetcode 11. Container With Most Water(逼近法)
11. Container With Most Water Medium Given n non-negative integers a1, a2, ..., an , where each repr ...
- 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
炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...
- [LeetCode] 11. Container With Most Water 装最多水的容器
Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ...
- [LeetCode]11. Container With Most Water 盛最多水的容器
Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ...
- [LeetCode] 11. Container With Most Water My Submissions Question 解题思路
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- 蜗牛慢慢爬 LeetCode 11. Container With Most Water [Difficulty: Medium]
题目 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai ...
- LeetCode#11. Container With Most Water
问题描述 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ...
随机推荐
- sso cas4.0改造历程--spring-webflow篇
https://blog.csdn.net/sinat_20689109/article/details/54910642
- Linux系统目录权限chmod误操作权限修复方法
Linux中,如果意外误操作将/目录权限批量设置,比如chmod -R 777 / ,系统中的大部分服务以及命令将无法使用,这时候可以通过系统自带的getfacl命令来拷贝和还原系统权限,若是其他系统 ...
- CentOS 7安装WordPress
在开始本文前,我假定你已经安装好了nginx.php-fpm和mariaDB(或mysql).它们的安装过程可参考我以前的文章. 1. 安装EPEL(Extra Packages for Enterp ...
- java基础-反射(细节)
java面试题--java反射机制? Java反射机制的作用:1)在运行时判断任意一个对象所属的类.2)在运行时判断任意一个类所具有的成员变量和方法.3)在运行时任意调用一个对象的方法4)在运行时构造 ...
- Alsa aplay S8 U8 S16_LE S16_BE U16_LE U16_BE格式
举个例子 aplay -r 16000 -f S16_LE -D hw:0,0 -c 2 -d 3 ~/Private/Private_Tools/02_ALSA_Learning/left_1k_r ...
- 使用Maven
MyEclipse2015自带有Maven,但是建个工程老出错. (Eclipse J2EE也自带有) 1.下载maven.官网 http://maven.apache.org/download.cg ...
- JavaWeb——jsp-config
<jsp-config> 包括 <taglib> 和 <jsp-property-group> 两个子元素.其中<taglib> 元素在JSP 1.2 ...
- python字符串前面的r/u/b的意义 (笔记)
u/U:表示unicode字符串 : 不是仅仅是针对中文, 可以针对任何的字符串,代表是对字符串进行unicode编码. r/R:非转义的原始字符串: 与普通字符相比,其他相对特殊的字符,其中可能包含 ...
- c#中使用excel
在做一个小项目,需要把一些查询结果导出到Excel,找了一些资料,自己也总结出了一点方法,与大家共享. 一.首先简要描述一下如何操作Excel表 先要添加对Excel的引用.选择项目-〉添加引用-〉C ...
- Rabbitmq(7) confirm模式
1. //将通道设置为comfirm模式channel.confirmSelect(); // 消息确认if(!channel.waitForConfirms()){ System.out.print ...