Question

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.

Solution

Since the area size is largely depended on shorter height, so we use two pointers. Time complexity O(n), space cost O(1).

 public class Solution {
public int maxArea(int[] height) {
if (height == null || height.length < 2)
return 0;
int left = 0, right = height.length - 1;
int result = 0, tmp = 0;
while (left < right) {
tmp = (right - left) * Math.min(height[left], height[right]);
result = Math.max(result, tmp);
if (height[left] <= height[right])
left++;
else
right--;
}
return result;
}
}

Container With Most Water 解答的更多相关文章

  1. 如何装最多的水? — leetcode 11. Container With Most Water

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

  2. [LintCode] Container With Most Water 装最多水的容器

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

  3. 67. Container With Most Water

    Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a poi ...

  4. LeetCode:Container With Most Water,Trapping Rain Water

    Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...

  5. No.011 Container With Most Water

    11. Container With Most Water Total Accepted: 86363 Total Submissions: 244589 Difficulty: Medium Giv ...

  6. leetcode面试准备:Container With Most Water

    leetcode面试准备:Container With Most Water 1 题目 Given n non-negative integers a1, a2, ..., an, where eac ...

  7. 关于Container With Most Water的求解

    Container With Most Water 哎,最近心情烦躁,想在leetcode找找感觉,就看到了这题. 然而,看了题目半天,硬是没看懂,于是乎就百度了下,怕看到解题方法,就略看了下摘要,以 ...

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

  9. Leetcode11 Container With Most Water 解题思路 (Python)

    今天开始第一天记录刷题,本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 准备按tag刷,第一个tag是array: 这个是array的第一道题:11. Container With ...

随机推荐

  1. DBA 经典面试题(4)

    1.如果信息采集管理系统(ICM)崩溃了怎么办?  答案:所有其他的管理器都会继续工作.ICM只会处理队列控制请求,意思是开启和关闭其他并发的管理器.    2.你如何加速打补丁的过程?    答案: ...

  2. 理解mcelog如何工作

    前言 本文,带你了解几个问题? 本文重点,主要看案例2,带你很好的理解mcelog如何工作的? mcelog的干什么的? mcelog 是 x86 的 Linux 系统上用来 检查硬件错误,特别是内存 ...

  3. SQLServer 循环1百万插入测试数据

    1,首先创建student表 create table student ( sno int primary key , sname VARCHAR(200) ) 2,--向数据库中插入100万条随机姓 ...

  4. C# 使用枚举获取对应的数组值时

    using System; enum Move { walk, run } class Program { static float[] speedAry = { 50.0f, 200.0f }; p ...

  5. 关于Latch

    Latch是什么 Latch是SQL Server引擎保证内存中的结构的一致性的轻量同步机制.比如索引,数据页和内部结构(比如非叶级索引页).SQL Server使用Buffer Latch保护缓冲池 ...

  6. Rss 的作用 及使用方法

    也可以参考http://jingyan.baidu.com/article/e73e26c0c73e1f24adb6a70f.html 什么是RSS RSS是站点用来和其他站点之间共享内容的一种简易方 ...

  7. oracle开启/关闭归档模式

    1.改变非归档模式到归档模式: 1)SQL> conn / as sysdba (以DBA身份连接数据库) 2)SQL> shutdown immediate;(立即关闭数据库) 3)SQ ...

  8. C#中静态类、静态方法和静态变量的简单说明

    静态方法与静态变量一样,属于类本身,而不属于哪个类的一个对象.调用一个被定义为static的方法,只有通过在它前面加上这个类的名称.一般定义里说是可以通过实例调用的.其他语言我还没测试,但是在C#中是 ...

  9. java.lang.NoClassDefFoundError: javax/xml/stream/XMLStreamException

    ?缺少jsr173_1.0_api.jar 包 或者jdk版本不对(包括工程和tomcat等服务器的jdk版本) 以前的一个xfire工程,今天重新导进后不能运行,修改工程的jdk版本不行,最后发现是 ...

  10. TextView之一:子类的常用属性

    TextView常见的子类包括EditText,Button,CheckBox, RadioButton等. 1.EditText EditText继承自TextView,因此TextView所有属性 ...