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.

问题:给定 n 个元素的数组 a1,a2..an,他们对应坐标中的 n 条线,第 i 条线的两端分别为坐标 (i, 0) 和 (i, ai)。求两根线,这两个线和 x 轴形成的容器能装最多的水。

这是一道直方图相关的题目,同样是直方图的题目还有:Trapping Rain Water 和 Largest Rectangle in Histogram。他们的解法有类似的地方,就是借助于递增元素以及对应的下标来计算。

  • 分别找出从左往右递增元素 Ls ,以及从右往左的递增元素 Rs 。
  • 用 Ls 的元素分别依次和 Rs 的元素组合形成容器,在所有结果中找到最大值就是原题目的解。

优化点:第二部做乘法时候,当 Ls[i] < Rs[k] 时候,Ls[i] 和 其他大于 Rs[k] 的组合可以不用再算,必然小于 Ls[i] 和 Rs[k] 。

     int maxArea(vector<int>& height) {

         if (height.size() < ){
return ;
} vector<int> idxAsceL; idxAsceL.push_back(); for (int i = ; i < height.size(); i++) { if (height[i] > height[idxAsceL.back()]) {
idxAsceL.push_back(i);
}
} vector<int> idxAsceR;
idxAsceR.push_back((int)height.size()-); for (int i = (int)height.size() - ; i >= idxAsceL.back(); i--) {
if (height[i] > height[idxAsceR.back()]) {
idxAsceR.push_back(i);
}
} int maxA = ;
for (int i = ; i < idxAsceL.size(); i++) {
for (int k = ; k < idxAsceR.size(); k++) { int l = idxAsceL[i];
int r = idxAsceR[k]; int h = min(height[l], height[r]); int len = r - l;
maxA = max(maxA, h * len); if (height[l] <= height[r]) {
break;
}
}
} return maxA;
}

[LeetCode] 11. Container With Most Water My Submissions Question 解题思路的更多相关文章

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

  2. Leetcode 11. Container With Most Water(逼近法)

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

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

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

  4. Java [leetcode 11] Container With Most Water

    问题描述: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ...

  5. LeetCode 11. Container With Most Water (装最多水的容器)

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

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

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

  7. LeetCode#11. Container With Most Water

    问题描述 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ...

  8. C#解leetcode 11. Container With Most Water

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

  9. [leetcode]11. Container With Most Water存水最多的容器

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

随机推荐

  1. 修改MySQL引擎

    1. 显示MySQL支持的引擎:show engines;

  2. .bash_profile和.bashrc的区别

    参考资料: http://blog.163.com/wang_hai_fei/blog/static/309020312008728333912/

  3. SoundPool 音频播放 详解 示例

    简介 如果应用程序经常播放密集.急促而又短暂的音效(如游戏音效)那么使用MediaPlayer显得有些不太适合了.因为MediaPlayer存在如下缺点: 1) 延时时间较长,且资源占用率高. 2) ...

  4. css position 应用(absolute和relative用法)

    1.absolute(绝对定位) absolute是生成觉对定位的元素,脱离了文本流(即在文档中已经不占据位置),参照浏览器的左上角通过top,right,bottom,left(简称TRBL) 定位 ...

  5. git变基、冲突解决

    参考git rebase 版本..变基 git冲突解决先fetch,pull,如果文件冲突,手动处理冲突文件,然后再fetch,pull,发现拉不下来,这时需要将文件改为已合并,然后提交文件 具体操作 ...

  6. 武汉科技大学ACM :1001: 零起点学算法34——继续求多项式

    Problem Description 输入1个正整数n, 计算1+(1+2)+(1+2+3)+...+(1+2+3+...+n) Input 输入正整数n(多组数据) Output 输出1+(1+2 ...

  7. a标签增加onclick事件提示未定义function

    项目使用的是ext框架,版本是ext4.2 出现的问题代码如下: renderer : function(value){ var html = "<a href=\"java ...

  8. javascript 关于一周前一个月前的处理方法

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. 国内开源html5游戏引擎全收录

    本文引自<国内开源html5游戏引擎全收录> 游戏开发这潭水太深,英文水平太差,不敢看国外的, 而且这几年国内技术水平也挺高了不少,特别是JS方面.(我个人感觉) 最近看了几个国产的js游 ...

  10. JSP九大隐式对象

    JSP九大隐式对象 request HttpServletRequest response HttpServletResponse session HttpSession application Se ...