leetcode_11. Container With Most Water

一,问题:

Given n non-negative integers a1a2, ..., an, where each represents a point at coordinate (iai). n vertical lines are drawn such that the two endpoints of line i is at (iai) 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 and n is at least 2.

翻译:

给定n个非负整数a1,a2,...,an,其中每个代表坐标(i,ai)处的一个点。 绘制n条垂直线,使得线i的两个端点处于(i,ai)和(i,0)处。 找到两条线,它们与x轴一起形成一个容器,以使容器包含最多的水。

注意:您不得倾斜容器(即木板效应),并且n至少为2。

二,思路:

1,暴力法:直接通过双重循环遍历,找出结果。

2,等值线法:其实还是针对法的翻版,找出n1对应的n2等值线,从而针对找寻对应获得最大值的n2。

三,代码:

1.V1:

func maxArea(height []int) int {
maxarea:=
for k:=;k<len(height)-;k++{
for k2:=k+;k2<len(height);k2++{
minheight:=height[k]
if height[k2]<height[k]{
minheight=height[k2]
}
smaxarea:=(k2-k)*int(minheight) if smaxarea>maxarea{
maxarea=smaxarea
}
}
}
return maxarea
}

Runtime:648ms,15.04%

根据上一次的经验,我将第二个遍历从右边开始。因为这样出现更大结果的可能性更高。

并且将内部一些方法提取出去。

最重要的是方法提取出去后,我可以节省一定的内存(如smaxarea)。

2.V2:

func maxArea(height []int) int {
maxarea:=
for k:=;k<len(height)-;k++{
for k2:=len(height)-;k2>k;k2--{
maxarea=max(maxarea,(k2-k)*min(height[k],height[k2]))
}
}
return maxarea
} func min(a,b int) int {
if a<b{
return a
}else{
return b
}
} func max(a,b int) int {
if a>b{
return a
}else{
return b
}
}

Runtime:592 ms,23.31%

虽然有所提高,但是依旧差距很大。所以必然存在巨大性能的提升点。

经过思考,我想到一个特性,当我在第二个循环内找到的高度比第一个循环内的高度高时,那么这次计算的结果必然是第二个循环这个循环中最大的结果,我就可以break了。这样我将会节省巨大的时间。

原因是我水桶矩形的高度最高也就是第一个循环中的高度(取最小值嘛),宽度必然是逐步减小的。(我是从两边往中间遍历的)

3.V3:

func maxArea(height []int) int {
maxarea:=
for k:=;k<len(height)-;k++{
for k2:=len(height)-;k2>k;k2--{ if height[k]<height[k2]{
maxarea=max(maxarea,(k2-k)*height[k])
break
}else{
maxarea=max(maxarea,(k2-k)*height[k2])
}
}
}
return maxarea
} func max(a,b int) int {
if a>b{
return a
}else{
return b
}
}

Runtime:112 ms,33.83%

也许比例提高不多,但是实际运行时间直接提高了五倍。这简直是跨越性的提升。

为了代码可读性,以及小的修改,让我简单地整理一下代码。

4.V4:

func maxArea(height []int) int {
maxarea:=
for left:=;left<len(height)-;left++{
for right:=len(height)-;right>left;right--{ if height[left]<height[right]{
maxarea=max(maxarea,(right-left)*height[left])
break
}else{
maxarea=max(maxarea,(right-left)*height[right])
}
}
}
return maxarea
} func max(a,b int) int {
if a>b{
return a
}else{
return b
}
}

Runtime: 104 ms,33.83%

小有提升,关键这样看起来很舒服。

四,他人代码:

1.最佳代码:

func min(a, b int) int {
if a < b {
return a
}else{
return b
}
} func max(a, b int) int {
if a < b {
return b
}else{
return a
}
} func maxArea(height []int) int{
area, left, right := , , len(height)-
for left < right {
h := min(height[left], height[right])
area = max(area, h*(right-left))
for ; left < right && height[left] <= h; left++{
}
for ; left < right && height[right] <= h; right--{
}
}
return area
}

Runtime:16ms,100%

2.分析:

其实这里面,有两处很精彩。

首先是循环判断条件 left<right。这个条件的精彩之处是它与之前代码中

 if height[left]<height[right]{
maxarea=max(maxarea,(right-left)*height[left])
break
}else{
maxarea=max(maxarea,(right-left)*height[right])
}

的良好配合。从两个方面节省了时间。可惜我只注意到了其在第二个循环的价值。并没有将之联系到第一个循环。在理解这点之后,我完善了自己的代码:

V5:

func maxArea(height []int) int {
length:=len(height)
left,right,maxarea:=,length-,
for ;left<right;left++{
for { if height[left]<=height[right]{
maxarea=max(maxarea,(right-left)*height[left])
break
}else{
maxarea=max(maxarea,(right-left)*height[right])
}
right--
}
}
return maxarea
} func max(a,b int) int {
if a>b{
return a
}else{
return b
}
}

Runtime: 20 ms,82.71%

这两个语句结合起来,就从内外两个循环节省时间了。需要好好理解。

(由于最近很忙,忙得有的博客都写得差不多了,却没时间修饰一下。估计之后博客会写得比较粗糙一些。)

leetcode_11. Container With Most Water的更多相关文章

  1. 如何装最多的水? — leetcode 11. Container With Most Water

    炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...

  2. [LintCode] Container With Most Water 装最多水的容器

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).  ...

  3. 67. Container With Most Water

    Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a poi ...

  4. LeetCode:Container With Most Water,Trapping Rain Water

    Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...

  5. No.011 Container With Most Water

    11. Container With Most Water Total Accepted: 86363 Total Submissions: 244589 Difficulty: Medium Giv ...

  6. leetcode面试准备:Container With Most Water

    leetcode面试准备:Container With Most Water 1 题目 Given n non-negative integers a1, a2, ..., an, where eac ...

  7. 关于Container With Most Water的求解

    Container With Most Water 哎,最近心情烦躁,想在leetcode找找感觉,就看到了这题. 然而,看了题目半天,硬是没看懂,于是乎就百度了下,怕看到解题方法,就略看了下摘要,以 ...

  8. [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: 找出数组中最大的数 ...

  9. Leetcode11 Container With Most Water 解题思路 (Python)

    今天开始第一天记录刷题,本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 准备按tag刷,第一个tag是array: 这个是array的第一道题:11. Container With ...

随机推荐

  1. Git忽略提交 .gitignore配置。自动生成IDE的.gitignore。解决gitignore不生效

    语法 以”#”号开头表示注释: 以斜杠“/”开头表示目录: 以星号“*”通配多个字符: 以问号“?”通配单个字符 以方括号“[]”包含单个字符的匹配列表: 以叹号“!”表示不忽略(跟踪)匹配到的文件或 ...

  2. 原生JS 将canvas生成图片

    核心代码: <script type="text/javascript"> // Converts image to canvas; returns new canva ...

  3. fiddler的inspectors传入的参数乱码

    问题描述:如图Q1所示.传入的参数存在中文乱码问题. 本机:win7 系统,解决方法如下 1.windows按钮+R 2.输入regedit  +回车+是 3.HKEY_CURRENT_USER\So ...

  4. java 控制流

    一:     块作用域 块(即复合语句):是指由一对花括号括起来的若干条简单的java语句.块决定了变量的作用域,一个块可以嵌套在另一个块中,如下: public class print_In { p ...

  5. 使用CSS3的“transition ”属性控制长宽度的缓慢变化

    有时候我们可能会想要改变某个资源信息的长宽度,比如改变某个div的宽度,而且需要让这个宽度缓慢改变,而不是突然就改变了.这时候你可能会想到使用jquery的animate()函数,不过这个方法既得引用 ...

  6. java学习笔记-基础篇

    Java基础篇 1—12 常识 13 this关键字 14参数传递 16 继承 17 访问权限 28—31异常 1—12 常识 1.文件夹以列表展示,显示扩展名,在地址栏显示全路径 2.javac编译 ...

  7. 关于Nanchtiy

    关于我 宇宙超级无敌蒟蒻一枚 爱玩游戏(LOL贼菜) 不爱刷题 不定时更新博客 福建某不知名市的某不知名高中的高二dog 偶尔看看动漫 欢迎加友链啦~ QQ:1468473741(会有妹子加吗?) 我 ...

  8. 构建 CDN 分发网络架构

    cdn基本架构: CDN的基本目的:1.通过本地缓存实现网站的访问速度的提升 CDN的关键点:CNAME在域名解析:split智能分发,引流到最近缓存节点

  9. ios开发UI篇—UIScrollView属性及其代理方法

    一.UIScrollView是什么? 1.UIScrollView是滚动的view,UIView本身不能滚动,子类UIScrollview拓展了滚动方面的功能. 2.UIScrollView是所有滚动 ...

  10. 关于NSStringFromClass的一点见解

    今天做项目的时候遇到一个需求,就是子view视图弹出时,屏蔽掉父view的所有手势,然后想到用 UIGestureRecognizerDelegate代理方法,中间省一些文字(无奈脸),言归正传,NS ...