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依赖注入使用详解 ...
随机推荐
- 谷歌浏览器,火狐浏览器,ie浏览器解析顺序
谷歌(版本 55.0.2883.87 m),火狐(48.0.2),ie(11.576.14393.0)对三个浏览器简单的进行了一下试验发现,谷歌浏览器是等到html全部解析完毕之后才开始渲染,而另外两 ...
- VS2015 调试Web项目 遭遇 HTTP 错误 500.23 - Internal Server Error
此错误是因为项目使用的托管管道模式有问题,将集成改为传统即可 选中项目 进入项目属性 ,如图界面
- PHP中Exception异常
异常的基本使用 当异常被抛出时,其后的代码不会继续执行,PHP 会尝试查找匹配的 "catch" 代码块. 如果异常没有被捕获,而且又没用使用 set_exception_hand ...
- Java程序员笔试、面试题目
1. 面向对象编程的三大特性是什么,请简要阐述 2. String 和StringBuffer的区别 3. 说出ArrayList,Vector, LinkedList的存储性能和特性 4. Coll ...
- CocoaPod 使用方法
huangyichengdeMacBook-Pro:~ Jack$ pod search AFNetworking/Library/Ruby/Site/2.0.0/rubygems.rb:250:in ...
- String字符串针对常量池的优化
String对象是java语言中重要的数据类型,但是不是基本数据类型.相对于c语言的char java做了一些封装和延伸. 针对常量池的优化:当两个String拥有相同的值时,它们只引用常量池中的同一 ...
- Java 中文乱码问题总结
开发java应用出现乱码是很常见的,毕竟现在unicode的使用还不是很广泛,在使用gb2312(包含了gbk简体,big5繁体)的系统中要正确 实现中文的display和数据库的存储是最基本的要求. ...
- EBS 中HOST主机并发请求模板
#!/bin/sh########################################################################################### ...
- js 中的快速排序算法简单实现
对于快速排序,最早是在c++中看到,它是利用指针来交换顺序,其实无论哪种语言,原理 和 思想都是一样,然而真正用起来的时候就特别容易忽略一些事实,导致实现失败.废话少说,下面用js实现一下快速排序: ...
- Windows Server 2008 R2 下配置证书服务器和HTTPS方式访问网站
http://www.cnblogs.com/zhongweiv/archive/2013/01/07/https.html 配置环境 了解HTTPS 配置CA证书服务器 新建示例网站并发布在IIS ...