leetcode Container With Most Water

原创 2013年12月02日 18:48:11

Container With Most Water

 Total Accepted: 2685 Total Submissions: 9008My Submissions

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.


This problem is different from largest rectangle in histogram. It's just a line instead of a histogram. So there is no water. 

class Solution {
 public:
  int maxArea(vector<int> &height) {
    int size = height.size(), l = 0, r = size - 1, res = 0;
    if (size == 0)
      return 0;
    while (l < r) {
      if (res < (r - l)*min(height[l],height[r]))
        res = (r - l)*min(height[l],height[r]);
      if (height[l] <= height[r])
        ++l;
      else
        --r;
    } 
    return res;
  }
};


【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
  • 11645

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
  • 1108

Leetcode Container With Most Water 解题报告

题目地址:http://oj.leetcode.com/problems/container-with-most-water/ 基本描述:x轴上在1,2,...,n点上有许多垂直的线段,长度依次是a1...
  • worldwindjp
  • worldwindjp
  • 2014年01月23日 10:22
  • 3484

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
  • 2233

【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
  • 402

[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
  • 1414

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
  • 3488

[C++]LeetCode: 39 Container With Most Water

题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinat...
  • cinderella_niu
  • cinderella_niu
  • 2014年12月02日 13:31
  • 539

【LeetCode-面试算法经典-Java实现】【011-ContainerWithMostWater(容纳最多的水)】

【011-ContainerWithMostWater(容纳最多的水)】找两条竖线然后这两条线以及X轴构成的容器能容纳最多的水。使用贪心算法   1.首先假设我们找到能取最大容积的纵线为 i, j (...
  • DERRANTCM
  • DERRANTCM
  • 2015年07月19日 06:58
  • 2669

[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
  • 1614
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode Container With Most Water
举报原因:
原因补充:

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