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. Robotium -- 使用JunitReport导出测试报告

    使用Robotium进行测试的时候,要想可以导出明了的测试结果,可以使用junitreport来实现 junit-report下载地址:https://github.com/jsankey/andro ...

  2. USACO lamps

    IOI 98称号.然后,它似乎没有很困难.即使我能做到这一点微弱的残留物.所有的button按两次不按,高达因此实际上总的等效按4二级,首先C往下<=4,则搜索将能直接照射,总共只有16状态(事 ...

  3. POJ 1250 Tanning Salon

    Tanning Salon Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6822   Accepted: 3686 Des ...

  4. Coding Your Life

    前几天看到篇文章,写的是科技让人变得陌生,balabala,总的说来就科技让邻居是男是女不知道了,朋友见面少了之类的.其实我觉得,也不能全怪科技发展的太快,而是人心都飘到网路上了,像我这一辈已经老去的 ...

  5. ssh链接云主机的一些笔记

    在window下 试了好几个ssh软件后用了putty,因为它免费而且中文不乱码 1.实现自动登陆功能 用快捷方式实现自动登陆 首先创建putty.exe的快捷方式到桌面:然后运行putty,输入ho ...

  6. Android自定义属性、控件三步法

    第二步中layout-activity_main.xml 中自命名控件: xmlns:android="http://schemas.android.com/apk/res/android& ...

  7. Java数据库连接之配置ODBC数据源

    java使用JDBC-ODBC桥接连接SQLServer数据库需要配置ODBC数据源,配置步骤如下: 1.进入控制面板,找到管理工具 2.看到ODBC数据源,有64位和32位的,如果你的数据库是64位 ...

  8. Ecstore内置表单验证?

       

  9. c - 给分数分级别

    /* 题目: 学习成绩>=90 分的同学用 A 表示, 80-89 分之间的用 B 表示,70-79 分的用 C 表示, 60-69 分用 D表示,小于60分用E表示. 分析: 使用swith. ...

  10. java JNI 的实现(1)-又进一步加深对JVM实现的理解

    目录 概述 主要优点 主要缺点 JNI实现的简单例子 开发工具 简略步骤 1,在eclipse的 'java类' 中声明一个 'native方法'; 2,使用 'javah' 命令生成包含'nativ ...