22.Container With Most Water(能装最多水的容器)
Level:
Medium
题目描述:
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
思路分析:
从两端往中间搜索,面积的计算方法为两端中较小的作为高,坐标差作为宽,然后求积就是面积,时间复杂度为O(n)。
代码:
public class Solution{
public int maxArea(int []height){
int i=0; //左端
int j=height.length-1;
int maxarea=Math.min(height[i],height[j])*(j-i);
while(i<j-1){
if(height[i]<=height[j])
i++; //为什么要i++而不是j++,因为要尽可能保留高度高的
else
j--;
int area=Math.min(height[i],height[j])*(j-i);
maxarea=Math.max(maxarea,area);
}
return maxarea;
}
}
22.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). ...
- 11. Container With Most Water[M]盛最多水的容器
题目 Given \(n\) non-negative integers \(a_1,a_2,\cdots,a_n\), where each represents a point at coordi ...
- [LeetCode] 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). ...
- lintcode:装最多水的容器
装最多水的容器 给定 n 个非负整数 a1, a2, ..., an, 每个数代表了坐标中的一个点 (i, ai).画 n 条垂直线,使得 i 垂直线的两个端点分别为(i, ai)和(i, 0).找到 ...
- [LintCode] Container With Most Water 装最多水的容器
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- LeetCode第十一题-可以装最多水的容器
Container With Most Water 问题简介:通过一个给定数组,找出最大的矩形面积 问题详解:给定一个数组,包含n个非负整数a1,a2,…,an,其中每个表示坐标(i,ai)处的点,绘 ...
- lintcode-383-装最多水的容器
383-装最多水的容器 给定 n 个非负整数 a1, a2, ..., an, 每个数代表了坐标中的一个点 (i, ai).画 n 条垂直线,使得 i 垂直线的两个端点分别为(i, ai)和(i, 0 ...
- LeetCode:盛最多水的容器【11】
LeetCode:盛最多水的容器[11] 题目描述 给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 ...
随机推荐
- [转] const T、const T*、T *const、const T&、const T*& 的区别
这里的T指的是一种数据类型,可以是int.long.doule等基本数据类型,也可以是自己类型的类型class.单独的一个const你肯定知道指的是一个常量,但const与其他类型联合起来的众多变化, ...
- CSV Format
[CSV Format] The comma separated values format (CSV) has been used for exchanging and converting dat ...
- jquery触发两次onchange事件
在项目中需要给select绑定改变事件,来触发动作,但是发现改变一次select,onchange方法执行两遍 //这是那个标签,用的是我们公司内部的selectlist <select cla ...
- php扩展开发2--添加类
1.需要实现的细节 实现一个person类 实现一个doing方法和saying方法 2.第一个扩展 2.1创建类的扩展: [root@bogon ext]# cd /usr/local/src/ph ...
- launchpad, jira, github
一.简介 http://segmentfault.com/q/1010000000165115
- jqentitydetail
using System;using System.Collections;using System.Collections.Generic;using System.Linq;using Syste ...
- DataTable 设置primarykey 后进行 Merge操作
1.先查看概念 可以看highplayer博客 http://blog.csdn.net/highplayer/article/details/6613817 2. protected void st ...
- OM Responsibility Flow
- C# Winform Cef 闪屏
C# Winform Cef 闪黑屏 CefSettings cs = new CefSettings(); cs.CefCommandLineArgs.Add(");//禁用GPU Cef ...
- C语言中无符号与有符号问题
unsigned char a[5] = { 12,36,96,128,182 }; a[]范围为0~256. 数组中数都有效. char a[5] = { 12,36,96,128,182 }; a ...