[LeetCode] 11. Container With Most Water My Submissions Question 解题思路
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.
问题:给定 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 解题思路的更多相关文章
- 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 用双指针向中间滑动,较小的高度就作为当前情 ...
- Leetcode 11. Container With Most Water(逼近法)
11. Container With Most Water Medium Given n non-negative integers a1, a2, ..., an , where each repr ...
- 如何装最多的水? — leetcode 11. Container With Most Water
炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...
- Java [leetcode 11] Container With Most Water
问题描述: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ...
- LeetCode 11. Container With Most Water (装最多水的容器)
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- [LeetCode] 11. Container With Most Water 装最多水的容器
Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ...
- LeetCode#11. Container With Most Water
问题描述 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ...
- C#解leetcode 11. Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- [leetcode]11. Container With Most Water存水最多的容器
Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ...
随机推荐
- 【移动开发】Android中将我们平时积累的工具类打包
Android开发的组件打包成JAR安装包,通过封闭成JAR包,可以重复利用,非常有利于扩展和减少工作重复性.这里为了讲解方便,我用了之前的一个代码框架中核心部分,不了解的可以回头看一下:http:/ ...
- C++学习笔记39:进程概念
进程的基本概念 进程是描述程序执行过程和资源共享的基本单位 主要目的:控制和协调程序的执行 进程相关函数 用户与组ID函数 创建进程:system(),fork(),exec() 终止进程:kill( ...
- Windows计划任务 未能启动
近期在windows server 2003上运行的备份脚本,在7月23日之后,没再运行,在计划任务里看到的状态是:未能启动.结果手动运行了一下备份脚本,没有问题,可以正常运行,但是在计划任务里为什么 ...
- Css Div半透明
用CSS控制外层DIV不透明,而内层DIV透明,这样实现的效果是意想不到的,还不错吧,其实代码也是很简单的,也很好理解,主要是用了CSS的滤镜. <html xmlns="http:/ ...
- Mysql 视图笔记2
这学期开了数据库的课,对sql注入颇感兴趣.因此,对数据库语句也颇为喜爱.遇到了with check option 问题.这属于sql view里面的一个问题.在此略做小结.大牛勿喷! 先自定义一个t ...
- JAVA-2-GetDay
import java.util.*; public class Ch0310 { public static void main(String[] args) { // TODO 自动生成的方法存根 ...
- IO流(文件字节输入输出
输入输出流可能有不允许操作,可能有出现错误,必须在try语句中进行 FileOutputStream out1=new FileOutputStream("test1.txt") ...
- JDK1.5新特性随手记
1.静态导入 import static 静态导入前写法: public class TestStatic { public static void main(String[] args) { Sys ...
- Win7下Solr4.10.1和IK Analyzer中文分词
1.下载IK中文分词压缩包IK Analyzer 2012FF_hf1,并解压到D:\IK Analyzer 2012FF_hf1: 2.将D:\IK Analyzer 2012FF_hf1\IKAn ...
- 设置CentOS里的Mysql开启客户端远程连接
CentOS系统安装好MySQL后,默认情况下不支持用户通过非本机连接上数据库服务器,下面是解决方法: 1.在控制台执行 mysql -u root -p mysql,系统提示输入数据库root用户的 ...