7 Container With Most Water_Leetcode
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.
这题O(n^2)的解法就是基本的暴力法。但是要想出O(n)的解法并不容易(实际上我就想不出来T_T),面试碰到这样难度的新题估计就跪了。
虽然有点悲观,但是解题的思路还是有迹可循的。
container的面积由两个因素组成,一个是高度,一个是宽度。
我们同样采用的是“按序枚举”的思路,高度是不确定的变量,不好枚举,但是宽度则是由1到n-1的确定的量,可以很方便的枚举。
此外,如同上题Candy,这种按序枚举的思路也可以是从左到右,从右往左等。
在想到对宽度进行枚举后,这种枚举有两种,一个是从小往大,另一个是从大往小。
如果我们想从小往大枚举,这种一般必须有dp的规律才能够使解法更优,但是从本题来看,每一个解都只与左右两个index有关,并不包含子问题。所以从小往大的枚举不可行。
那么从大往小枚举,比如3,2,5,4,1。最大的宽度,就是从3到1,这时我们可以算出一个面积。当我们缩小这个宽度,有两种方法,但是实际上只有右区间缩小一个可能得到最优解。为什么呢?因为每次对于左右边界中较小的那一个,当前的宽度都是最宽的,也就是它能达到的最大面积了。
由此我们可以只往一个方向进行左右边界的缩小,最终得到的方法是O(n)的。
Code:
class Solution {
public:
int maxArea(vector<int> &height) {
int n = height.size();
if(n < 2) return 0;
int res = 0; int left = 0, right = n-1;
while(left < right)
{
int tmp = min(height[left], height[right]) * (right - left);
if(tmp > res) res = tmp; if(height[left] < height[right]) left++;
else right--;
}
return res;
}
};
7 Container With Most Water_Leetcode的更多相关文章
- 在docker中运行ASP.NET Core Web API应用程序(附AWS Windows Server 2016 widt Container实战案例)
环境准备 1.亚马逊EC2 Windows Server 2016 with Container 2.Visual Studio 2015 Enterprise(Profresianal要装Updat ...
- .Container与.container_fluid区别
.Container与.container_fluid是bootstrap中的两种不同类型的外层容器,两者的区别是:.container 类用于固定宽度并支持响应式布局的容器..container-f ...
- View and Data API Tips: Constrain Viewer Within a div Container
By Daniel Du When working with View and Data API, you probably want to contain viewer into a <div ...
- [LeetCode] 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
炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...
- Docker - command in docker container
1.查看Container 里面运行的进程 在运行容器以后,可以查看里面的进程: docker top <container_id> or <container_name> 2 ...
- 【leetcode】Container With Most Water
题目描述: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ...
- [LintCode] Container With Most Water 装最多水的容器
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- ASP.NET MVC中使用Unity Ioc Container
写在前面 安装Unity 添加服务层 IArticleRepository类型映射 服务注入到控制器 Global.asax初始化 后记 关于Unity的使用可以参照<Unity依赖注入使用详解 ...
随机推荐
- Mybatis学习总结(一)——入门基础
一.Mybatis介绍 1.MyBatis是什么? MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了go ...
- RabbitMQ之window安装步骤
安装Rabbit MQ Rabbit MQ 是建立在强大的Erlang OTP平台上,因此安装Rabbit MQ的前提是安装Erlang.通过下面两个连接下载安装3.2.3 版本: 下载并安装 Era ...
- 集中日志服务器Rsyslog
http://www.gaizaoren.org/archives/408 基于主机的管理一般需要收集服务器的日志信息用于及时发现错误,处理故障. 搭建linux下的集中日志服务器的程序一般可以用sy ...
- “fatal error C1010”错误解决的三种方法
尝试写了一个简单的类文件,但在编译的时候提示错误,具体错误信息如下: fatal error C1010: unexpected end of file while looking for preco ...
- MySQL日志
在MySQL中共有4中日志:错误日志.二进制日志.查询日志和慢查询日志 一.错误日志 错误日志名 host_name.err,并默认在参数DATADIR指定的目录中写入日志文件.可使用 --log-e ...
- c++虚析构函数
虚析构函数的作用主要是当通过基类指针删除派生类对象时,调用派生类的析构函数(如果没有将不会调用派生类析构函数) #include <iostream> using namespace st ...
- final和包装类
==================================================================================================== ...
- nova instance出错:"message": "Proxy error: 502 Read from server failed
执行 $ nova resize instance1 时候出错: {, "details": " File \"/opt/stack/nova/nova/com ...
- java并行计算Fork和Join的使用
Java在JDK7之后加入了并行计算的框架Fork/Join,可以解决我们系统中大数据计算的性能问题.Fork/Join采用的是分治法,Fork是将一个大任务拆分成若干个子任务,子任务分别去计算,而J ...
- 在桌面程序上和Metro/Modern/Windows store app的交互(相互打开,配置读取)
这个标题真是取得我都觉得蛋疼..微软改名狂魔搞得我都不知道要叫哪个好.. 这边记录一下自己的桌面程序跟windows store app交互的过程. 由于某些原因,微软的商店应用的安全沙箱导致很多事情 ...