描述:

实现1 -- 求所有可能的值,O(N^2),超时了(因为超时没有跑所有的测试用例,所以不确定还有没有其他问题)

代码:

     def maxArea(self, height):
tmp = len(height)
if tmp == 0 or tmp == 1: return 0
if tmp == 2: return abs(height[1] - height[0]) minus_lst = [height[i] - height[i-1] for i in range(1, len(height))]
# len(minus_lst) >= 2 dis, max, l = 2, 0, len(minus_lst) while True:
for i in range(l):
if i + dis > l: break area = abs(sum(minus_lst[i:i+dis]) * dis)
if area > max: max = area dis += 1
if dis > l: break return max

实现2:

对于每一个节点,取比他长的节点的位置,此时area = 距离差 * 这个节点的高度,实践复杂度 O(N^2),超时

代码:

     def maxArea(self, height):
tmp = len(height)
result = 0 for i in range(tmp):
for j in range(tmp):
if height[j] >= height[i]:
area = height[i] * abs(i - j)
if area > result: result = area return result

优化-1 依然超时

     def maxArea(self, height):
tmp = len(height)
result = 0 record = []
for i in range(tmp):
for j in range(tmp):
if height[j] >= height[i]:
record.append(abs(j - i))
area = height[i] * max(record)
if area > result: result = area
record = [] return result

优化-2 超时

     def maxArea(self, height):
tmp = len(height)
result = 0 for i in range(tmp):
t = None
for j in range(i):
if height[j] > height[i]:
t = abs(j - i)
break if t is not None:
area_1 = t * height[i]
if area_1 > result: result = area_1 t = None
for j in range(i, tmp)[::-1]:
if height[j] > height[i]:
t = abs(j - i)
break if t is not None:
area_2 = t * height[i]
if area_2 > result: result = area_2 return result

实现3

从左端点和右端点开始,贪婪,不断取小的值推进,符合直觉,如何证明可以使用贪婪

当左边是i右边是j时,ij之间不会有更大的,两边?

todo

#LeetCode# Container With Most Water (todo)的更多相关文章

  1. Container With Most Water(LintCode)

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

  2. 【leetcode】Container With Most Water(middle)

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

  3. Leetcode Tags(3)String(TODO)

    一.Easy 696 Count Binary Substrings Input: "00110011" Output: 6 Explanation: There are 6 su ...

  4. C#刷遍Leetcode面试题系列连载(1) - 入门与工具简介

    目录 为什么要刷LeetCode 刷LeetCode有哪些好处? LeetCode vs 传统的 OJ LeetCode刷题时的心态建设 C#如何刷遍LeetCode 选项1: VS本地Debug + ...

  5. C#刷遍Leetcode面试题系列连载(2): No.38 - 报数

    目录 前言 题目描述 相关话题 相似题目 解题思路: 运行结果: 代码要点: 参考资料: 文末彩蛋 前言 前文传送门: C# 刷遍 Leetcode 面试题系列连载(1) - 入门与工具简介 上篇文章 ...

  6. C#刷遍Leetcode面试题系列连载(4) No.633 - 平方数之和

    上篇文章中一道数学问题 - 自除数,今天我们接着分析 LeetCode 中的另一道数学题吧~ 今天要给大家分析的面试题是 LeetCode 上第 633 号问题, Leetcode 633 - 平方数 ...

  7. C#刷遍Leetcode面试题系列连载(5):No.593 - 有效的正方形

    上一篇 LeetCode 面试题中,我们分析了一道难度为 Easy 的数学题 - 自除数,提供了两种方法.今天我们来分析一道难度为 Medium 的面试题. 今天要给大家分析的面试题是 LeetCod ...

  8. C# 刷遍 Leetcode 面试题系列连载(3): No.728 - 自除数

    前文传送门: C#刷遍Leetcode面试题系列连载(1) - 入门与工具简介 C#刷遍Leetcode面试题系列连载(2): No.38 - 报数 系列教程索引 传送门:https://enjoy2 ...

  9. leetcode 1.回文数-(easy)

    2019.7.11leetcode刷题 难度 easy 题目名称 回文数 题目摘要 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 思路 一些一定不为回文数的 ...

随机推荐

  1. Firefly distributed模块的原理与twisted中PB远程调用协议

    这些天断断续续在看Firefly, 看了一下distributed模块的设计,其实就是使用的twisted.spread.pb觉得以后要是想用Firefly有必要了解一下twisted, 所以在网上查 ...

  2. Docker intercontainer 网络解释

    https://segmentfault.com/a/1190000000669312

  3. mysql事务回滚

    首先条件是表要设置为 InnoDB  类型. 当在一个库连接中,通过调用另一个 库名称.表名称,可以回滚: 当用USE dbName后,在两个或多个库操作时,一次只能回滚一个库中的东西: 当在多个数据 ...

  4. 【转】使用BBB的device tree和cape(重新整理版)

    只要你想用BBB做哪怕一丁点涉及到硬件的东西,你就不可避免地要用到cape和device tree的知识.所以尽管它们看起来很陌生而且有点复杂,但还是得学.其实用起来不难的.下面我只讲使用时必须会的内 ...

  5. Android更改imagebutton为纯色方法

    我的imagebutton所用的背景png图片是灰色的,但是我想让他显示出来是白色的按钮,如果用ps去一个个填充不太现实,那有没有什么办法去通过xml里的属性改变背景颜色呢? 一开始我用了网上的方法 ...

  6. [置顶] c++,vc6.0,中友元函数,无法访问私有字段(private)的问题(problem),cannot access private member declared in class 'Date'

    c++,vc6.0,中友元函数,无法访问私有字段(private)的问题(problem),cannot access private member declared in class 'Date' ...

  7. 关于JFace中的向导式对话框(WizardDialog类)

    向导式对话框是一种非常友好的界面,它能够引导用户一步步的输入信息.Eclipse的"新建项目",就是这样的向导式对话框. 在Eclipse中向导式对话框的开发是很简单的,它由Wiz ...

  8. 关于Spring的IOC和DI

    原始调用模型 Spring的演化过程 Spring的调用过程 ======================================= IoC[理解][应用][重点] 1.IoC(Inversi ...

  9. .net 邮件批量发送功能源码

    #define debug   using System; using System.Text; using System.Linq; using System.IO; using System.Ne ...

  10. PowerDesigner使用教程 —— 概念数据模型 (转)

    一.概念数据模型概述    概念数据模型也称信息模型,它以实体-联系(Entity-RelationShip,简称E-R)理论为基础,并对这一理论进行了扩充.它从用户的观点出发对信息进行建模,主要用于 ...