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 Q11:Container With Most Water

题目11: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (...

LeetCode_Container With Most Water

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

【LeetCode】Container With Most Water

参考链接 题目描述 题目分析 总结 代码示例 推荐学习C++的资料 C++标准函数库 http://dow...

LeetCode | Container With Most Water

题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinat...

LeetCode011 Container With Most Water

详细见:leetcode.com/problems/container-with-most-water/ Java Solution: github package leetcode;...
  • zxwtry
  • zxwtry
  • 2017年03月28日 17:21
  • 76

LeetCode11. Container With Most Water

11. Container With Most Water 1、原题 Given n non-negative integers a1, a2, ..., an, where each...

LeetCode11. Container With Most Water(思维题:选择左右边使得容器所盛水最多)

这道题,我独立想了好久!!!!一道很好的思维题!!!!!!!! 思路1是自己的求解方法O(nlogn),思路2是网上最优的解法O(n),暴利O(n^2)肯定超时! 题目链接:htt...

LeetCode: Container With Most Water [Java]

Description: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordi...

[LeetCode]11.Container With Most Water

【题目】 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (...

leetcode#11.Container With Most Water

一开始的代码nt maxArea(int* height, int heightSize) { int max = 0; if(heightSize == 0 || heightSiz...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode Container With Most Water
举报原因:
原因补充:

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