LeetCode 11. Container With Most Water(java)

原创 2018年01月15日 08:48:20

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.

思路:由于只需要确定两个竖线的位置,求两个竖线之间的容量,因此,用双指针的想法,两端向中间逼近,每次逼近的方法是,如果左边竖线更高,就end–,如果右边竖线更高,就begin++。

public int maxArea(int[] height) {
        if (height.length <= 1) return 0;
        int begin = 0, end = height.length - 1, max = 0;
        while (begin < end) {
            int area = Math.min(height[begin], height[end]) * (end - begin);
            if (area > max) max = area;
            if (height[begin] > height[end]) {
                end--;
            } else {
                begin++;
            }
        }
        return max;
    }

IEEE 802.11b标准简析

IEEE 802.11b标准简析(作者:李伟 2001年02月14日 09:44)  以往,无线局域网发展缓慢,推广应用困难,主要是由于传输速率低、成本高、产品系列有限,且很多产品不能相互兼容。如以前...
  • ghj1976
  • ghj1976
  • 2001-02-16 16:25:00
  • 2125

[LeetCode][11]Container With Most Water解析 时间复杂度为O(n) -Java实现

Q: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate...
  • u014629433
  • u014629433
  • 2016-06-12 14:16:19
  • 1577

[LeetCode]Container With Most Water, 解题报告

前言 难怪LeetCode OJ在找工作时被很多人推荐,发现了这道最大蓄水题目就是美团的笔试最后一道题,当时我霸笔只有着一道题目没有答出来,因此也就没有获得面试机会,可惜了 题目 Given n ...
  • zinss26914
  • zinss26914
  • 2013-12-10 19:47:10
  • 20082

[LeetCode] 011. Container With Most Water (Medium) (C++/Java/Python)

[LeetCode] 011. Container With Most Water (Medium) (C++/Java/Python)
  • hcbbt
  • hcbbt
  • 2015-03-02 22:51:59
  • 1682

LeetCode --- 11. Container With Most Water

题目链接:Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents...
  • makuiyu
  • makuiyu
  • 2015-01-29 10:49:05
  • 2505

LeetCode 11 Container With Most Water (C,C++,Java,Python)

Problem: Given n non-negative integers a1, a2, ..., an, where each represents a point at coor...
  • runningtortoises
  • runningtortoises
  • 2015-05-07 22:56:04
  • 1206

LeetCode(11) ContainerWithMostWater

题目如下: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i,...
  • feliciafay
  • feliciafay
  • 2013-12-09 08:23:48
  • 3599

【LEETCODE】11-Container With Most Water [Python]

Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai)...
  • aliceyangxi1987
  • aliceyangxi1987
  • 2016-01-07 09:03:13
  • 486

【LeetCode】Container With Most Water 解题报告

【题目】 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordina...
  • ljiabin
  • ljiabin
  • 2014-12-02 09:24:29
  • 12390

LeetCode 11. Container With Most Water(容器装水)

原题网址:https://leetcode.com/problems/container-with-most-water/ Given n non-negative integers a1,...
  • jmspan
  • jmspan
  • 2016-05-19 05:24:14
  • 523
收藏助手
不良信息举报
您举报文章:LeetCode 11. Container With Most Water(java)
举报原因:
原因补充:

(最多只允许输入30个字)