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.


题目标签:Array

  这道题目给了我们一个height array, 每一个数字代表一条竖直线的高度,从x轴开始。一开始用暴力法没通过。所以要用特别方法来做。分析一下,如何才能够拿到更多的水。如果一根线很低,另外一根很高,那么水只能装到低的那根线,等于把高出的区域浪费了。那么需要找到更高的线来替代低的那条才有希望找到装水更多的两条线。设两个pointer,left 和 right。 从最两边开始遍历,每一次都计算面积,比max大的话就替换。接着把低的那根线的pointer 向另外一边移动,直到两个pointers 相遇。

Java Solution:

Runtime beats 31.31%

完成日期:07/11/2017

关键词:Array

关键点:Two Pointers

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

参考资料:N/A

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

LeetCode 11. Container With Most Water (装最多水的容器)的更多相关文章

  1. [LeetCode] 11. Container With Most Water 装最多水的容器

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

  2. [LeetCode]11. Container With Most Water 盛最多水的容器

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

  3. [LeetCode] Container With Most Water 装最多水的容器

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

  4. 【LeetCode】11. Container With Most Water 盛最多水的容器

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:盛水,容器,题解,leetcode, 力扣,python ...

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

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

  6. 11. Container With Most Water(装最多的水 双指针)

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

  7. 011 Container With Most Water 盛最多水的容器

    给定 n 个非负整数 a1,a2,…,an,每个数代表坐标中的一个点 (i, ai) .画 n 条垂直线,使得垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的两条线,使得它们 ...

  8. Leetcode11.Container With Most Water盛最多水的容器

    给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的两条线, ...

  9. lintcode:装最多水的容器

    装最多水的容器 给定 n 个非负整数 a1, a2, ..., an, 每个数代表了坐标中的一个点 (i, ai).画 n 条垂直线,使得 i 垂直线的两个端点分别为(i, ai)和(i, 0).找到 ...

  10. 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 用双指针向中间滑动,较小的高度就作为当前情 ...

随机推荐

  1. 小甲鱼:Python学习笔记001_变量_分支_数据类型_运算符等基础

    1.下载并安装Python 3.5.0 Python是一个跨平台语言,Python从3.0的版本的语法很多不兼容2版本,官网找到最新的版本并下载:http://www.python.org 2.IDL ...

  2. 静态页面如何实现 include 引入公用代码

    一直以来,我司的前端都是用 php 的 include 函数来实现引入 header .footer 这些公用代码的,就像下面这样: <!-- index.php --> <!DOC ...

  3. 新版本mac 无法打开第三方应用

    新版本mac 没有任何应用可以打开的这个选项 现在解决方法已经找到 特此标记一下 1打开终端 2 输入 sudo spctl --master-disable 3.打开系统设置的中的安全即可出现

  4. Message:Unable to locate element 问题解决方法

    Python断断续续学了有一段时间了,总感觉不找个小项目练练手心里没底,哪成想出门就遇到"拦路虎",一个脚本刚写完就运行报错,还好做足了心里准备,尝试自行解决. 或许网上有相关解决 ...

  5. Linux入门之常用命令(13) crontab

    为当前用户创建cron服务 1.  键入 crontab  -e 编辑crontab服务文件 例如 文件内容如下: */2 * * * * /bin/sh /home/admin/jiaoben/bu ...

  6. Beautiful Dream hdu3418 (直接做或二分)

    Beautiful Dream Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  7. NOIP2017SummerTraining0714

    个人感受:第一题做了字典树,还运行错误,然后就弃疗了,然后水了二三两题,总共拿了85分,倒数. 正确答案 时间限制: 2 Sec  内存限制: 256 MB提交: 702  解决: 82[提交][状态 ...

  8. 一个高性能、轻量级的分布式内存队列系统--beanstalk

    Beanstalk是一个高性能.轻量级的.分布式的.内存型的消息队列系统.最初设计的目的是想通过后台异步执行耗时的任务来降低高容量Web应用系统的页面访问延迟.其实Beanstalkd是典型的类Mem ...

  9. python修改注册表

    与注册表操作相关的函数可以分为打开注册表.关闭注册表.读取项值.c添加项值.添加项,以及删除项等几类. 表1   Windows注册表基本项 项名 描述 HKEY_CLASSES_ROOT 是HKEY ...

  10. Python数据分析(二): Pandas技巧 (1)

    第一部分: ipython http://www.cnblogs.com/cgzl/p/7623347.html 第二部分: numpy http://www.cnblogs.com/cgzl/p/7 ...