[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). ...
随机推荐
- USACO lamps
IOI 98称号.然后,它似乎没有很困难.即使我能做到这一点微弱的残留物.所有的button按两次不按,高达因此实际上总的等效按4二级,首先C往下<=4,则搜索将能直接照射,总共只有16状态(事 ...
- POJ 1250 Tanning Salon
Tanning Salon Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6822 Accepted: 3686 Des ...
- 跨平台传输中使用base64来保证非ascii码字符串的完整性
首先,我们来看一个例子: byte[] b=new byte[]{2,9,43}; String ss=new String(b,"utf-8"); byte[] b1=ss.ge ...
- C#导出数据到Excel通用的方法类
导出数据到Excel通用的方法类,请应对需求自行修改. 资源下载列表 using System.Data; using System.IO; namespace IM.Common.Tools { p ...
- HashSet与HashMap
HashSet底层由HashMap实现 Hash表:存放链表表头的数组 HashSet的值存放于HashMap的key上,而HashMap的value统一为PRESENT(private static ...
- 为什么要配置path环境变量?
一:关于path环境变量--为了在任意目录下,使用javac/java命令 第一种配置方法: 通过配置path环境变量,我们可以使某个程序,比如javac.exe,在任意目录下都可以运行,而不用跑到j ...
- AS 自动生成选择器 SelectorChapek
简介 https://github.com/inmite/android-selector-chapek 设计师给我们提供好了各种资源,每个按钮都要写一个selector是不是很麻烦? 这么这个插件就 ...
- SpringMVC06以对象的方式获取前台的数据
========创建需要的两个实体类================ public class School { private String sName; private String addres ...
- MySQL -A不预读数据库信息(use dbname 更快)
mysql数据库预读与不预读数据库信息(use dbname)—Reading table information for completion of table and column names ...
- 转自:http://blog.sina.com.cn/s/blog_86e874d30101e3d8.html(谢谢原文作者),Win7下安装CentOS 6.5双系统
经过一下午的折腾,终于在64位的Windows 7上面成功安装了CentOS 6.5(64bit)系统,中途因为硬盘分区的问题失败了一次.下面是安装过程: 在安装过程中借助了这篇文章的内容:http: ...