关于Container With Most Water的求解
Container With Most Water
哎,最近心情烦躁,想在leetcode找找感觉,就看到了这题。
然而,看了题目半天,硬是没看懂,于是乎就百度了下,怕看到解题方法,就略看了下摘要,以为懂了,万万没想到,居然把题目意思理解错了,所以还是要好好学英语呀。
然而,在理解对了之后,又是time limit excess,又瞟了下博客讲解,也算是徒手码了出来,记录一发,随便再体会下....
题目相关
题目在这,Container With Most Water
大概意思就是,给出的各点作x轴垂线,与x轴组成一个Container,然后找出两条线,使得其能储存最大容量的水,哎,其实仔细看题目,人家表述的很清楚呀,好好学英语呀...
思路一
正确理解题目了之后,按照我等弱鸡智商当然是第一反应是遍历了,很简单,两边两块板,容量为:
min*(j-i)
选取一边作为基准,遍历其后面的所有边,与之形成容器,得到各个容量。
两个循环就行...
好久没用js了,就熟悉下。
Algorithm = {};
Algorithm.private = {};
Algorithm.private.GetArea = function (height, i, j) {
var min = 0;
if (height[i] > height[j]) {
min = height[j];
} else {
min = height[i];
}
return min * (j - i);
}
Algorithm.ContainerWater = function (height) {
var maxArea = 0;
for (var i = 0; i < height.length; i++) {
for (var j = i; j < height.length; j++) {
var t = Algorithm.private.GetArea(height, i, j)
if (maxArea < t) {
maxArea = t;
}
}
}
return maxArea;
}
毫无疑问,时间超时,不过好歹也能工作吧.....
思路二
看了下别人的思路,原来,还可以这样
最终找到了两条线应该具备以下两个性质
- 左边的线的左边不会有比其更长的线
- 右边的线的右边不会有比其更长的线
其实很好理解,如果所找到的结果不满足这两条性质,那这结果必然不是最优的,也即是说,上面两点是必要条件,由此,得到以下代码...
Algorithm.ContainerWaterOpt = function (height) {
var a = [], b = [], amax = 0, bmax = 0, maxArea = 0;
for (var i = 0; i < height.length; i++) {
if (amax < height[i]) {
amax = height[i];
a.push(i);
}
if (bmax < height[height.length - i - 1]) {
bmax = height[height.length - i - 1];
b.push(height.length - i - 1);
}
}
for (var i = 0; i < a.length; i++) {
for (var j = 0; j < b.length; j++) {
var t = Algorithm.private.GetArea(height, a[i], b[j]);
if (maxArea < t) {
maxArea = t;
}
}
}
return maxArea;
}
后记
哎,以后做题时,还是好好思考题目本身里面的内涵,不一定非要从数据结构等来思考...
最最重要的,好好学英语呀!
关于Container With Most Water的求解的更多相关文章
- 如何装最多的水? — leetcode 11. Container With Most Water
炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...
- Container With Most Water - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Container With Most Water - LeetCode 注意点 没什么好注意的... 解法 解法一:暴力求解,假设任意两个端点会是最佳答 ...
- [LintCode] Container With Most Water 装最多水的容器
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- 67. Container With Most Water
Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a poi ...
- LeetCode:Container With Most Water,Trapping Rain Water
Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...
- No.011 Container With Most Water
11. Container With Most Water Total Accepted: 86363 Total Submissions: 244589 Difficulty: Medium Giv ...
- leetcode面试准备:Container With Most Water
leetcode面试准备:Container With Most Water 1 题目 Given n non-negative integers a1, a2, ..., an, where eac ...
- [leecode]---11.container with most water
description: Input: [1,8,6,2,5,4,8,3,7]Output: 49 思路1: 从(1,a1)开始向后算面积,需要两层n循环,时间复杂度n2 思路2: 找出数组中最大的数 ...
- Leetcode11 Container With Most Water 解题思路 (Python)
今天开始第一天记录刷题,本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 准备按tag刷,第一个tag是array: 这个是array的第一道题:11. Container With ...
随机推荐
- 编写Windows Service 备忘
项目需求要做一个定时扫表,将按条件查询到的数据插入或者更新到另一个数据表的需求,老大要求让用window service来做 因为以前没有做过,把这次的经历写出来.作为备忘. 1.什么是windows ...
- Node填坑教程——整理文件
如果你能把所有代码写进一个文件,并且能很好的管理.协调.多人开发.那么可以跳过这期(请务必带我飞). 我们接着完善上期的代码,给代码分家. node并没有启动入口,更没有固定的项目结构,文件配置.这些 ...
- Asp.Net异步编程
Asp.Net异步编程-使用了异步,性能就提升了吗? Asp.Net异步编程 写在前面的话,很久没有写Blog了,不对,其实一致就没有怎么写过.今天有空,我也来写一篇Blog 随着.Net4.5的推出 ...
- AIX errpt命令说明
查看系统的错误记录 在系统运行时,一些系统错误会记录在errlog 中,其中有些错误还会在终端上显示.检查错误日志可用以下命令: # errpt IDENTIFIER TIMESTAM P T ...
- C#接口总结
C#接口总结 浅析C#接口特点及实例应用 C#接口(interface)的掌握对我们开发有什么作用呢?C#接口的使用能够使我们的程序有什么改进?那么我们首先我们来看看C#接口特点以及具体的实例使用分析 ...
- [转]OpenSolaris 2009.06, dev setup
Source:http://seriouswebapps.com/opensolaris-200906-dev-setup 1. Snapshot the root zfs pool This all ...
- TPL相关
C#中的Timer System.Windows.Forms.TimerSystem.Threading.Timer System.Timers.Timer 1.System.Windows.Form ...
- AOP的成员介绍
AOP(Aspect Oriented Programming)面向切面编程,AOP的作用不过多介绍,本文是主要是介绍AOP的成员,是我在复习的时候记录的一些笔记,方便以后查阅方便一些. JointP ...
- 有一个array的数组,长度为10000,大小不一,用算法找出该数组中的最大值。
不用算法的答案是: var a=[1,2,3,5……];alert(Math.max.apply(null, a));//最大值alert(Math.min.apply(null, a));//最 ...
- Page_Load接收随机参数放到字典类中
Page_Load接收随机参数放到字典类中,可以用作签名.普通的接收url的参数可以用作下面这种模式: int appid =Convert.ToInt32(param["appid&qu ...