#LeetCode# Container With Most Water (todo)
描述:
实现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)的更多相关文章
- Container With Most Water(LintCode)
Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a poi ...
- 【leetcode】Container With Most Water(middle)
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- Leetcode Tags(3)String(TODO)
一.Easy 696 Count Binary Substrings Input: "00110011" Output: 6 Explanation: There are 6 su ...
- C#刷遍Leetcode面试题系列连载(1) - 入门与工具简介
目录 为什么要刷LeetCode 刷LeetCode有哪些好处? LeetCode vs 传统的 OJ LeetCode刷题时的心态建设 C#如何刷遍LeetCode 选项1: VS本地Debug + ...
- C#刷遍Leetcode面试题系列连载(2): No.38 - 报数
目录 前言 题目描述 相关话题 相似题目 解题思路: 运行结果: 代码要点: 参考资料: 文末彩蛋 前言 前文传送门: C# 刷遍 Leetcode 面试题系列连载(1) - 入门与工具简介 上篇文章 ...
- C#刷遍Leetcode面试题系列连载(4) No.633 - 平方数之和
上篇文章中一道数学问题 - 自除数,今天我们接着分析 LeetCode 中的另一道数学题吧~ 今天要给大家分析的面试题是 LeetCode 上第 633 号问题, Leetcode 633 - 平方数 ...
- C#刷遍Leetcode面试题系列连载(5):No.593 - 有效的正方形
上一篇 LeetCode 面试题中,我们分析了一道难度为 Easy 的数学题 - 自除数,提供了两种方法.今天我们来分析一道难度为 Medium 的面试题. 今天要给大家分析的面试题是 LeetCod ...
- C# 刷遍 Leetcode 面试题系列连载(3): No.728 - 自除数
前文传送门: C#刷遍Leetcode面试题系列连载(1) - 入门与工具简介 C#刷遍Leetcode面试题系列连载(2): No.38 - 报数 系列教程索引 传送门:https://enjoy2 ...
- leetcode 1.回文数-(easy)
2019.7.11leetcode刷题 难度 easy 题目名称 回文数 题目摘要 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 思路 一些一定不为回文数的 ...
随机推荐
- (原)linux 编译 lwqq
1.安装工具 apt-get install automake apt-get install autoconf apt-get install libtool apt-get install lib ...
- error: /usr/include/objc/objc-class.h: No such file or directory
When i use the example of ShareKit package,i have come across this error:"error: /usr/include/o ...
- boot2docker安装及使用
更新homebrew 为了确保有boot2docker的安装脚本 brew update 安装boot2docker brew install boot2docker 初始化 boot2docker ...
- 不使用Math.random实现随机数。
var rand = (function(){ var today = new Date(); var seed = today.getTime(); function rnd(){ seed = ( ...
- Greedy
在现实世界中,有这样一类问题:它有n个输入,而它的解就由这n个输入的某个子集组成,不过这个子集必须满足某些事先给定的条件.把那些必须满足的条件称为约束条件:而把满足约束条件的子集称为该问题的可行解.问 ...
- 【转载】最近在用Arrays的asList()生成的List时,List元素的个数时而不正确,数组转化为List,即Arrays.asList(intArray);
最近在用Arrays的asList()生成的List时,List元素的个数时而不正确. Java代码 //经多次测试,只要传递的基本类型的数组,生成List的元素个数均为1 char arrc = { ...
- JAXB - XML Schema Types, Defining an Enumeration
If you want a data type that enumerates discrete values you should use a restriction of the schema t ...
- Spring分布式事务实现(适用于spring-tx 2.5)
http://log-cd.iteye.com/blog/807607 分布式事务是指操作多个数据库之间的事务,spring的org.springframework.transaction.jta.J ...
- Jquery方法大全
一.JQuery常用的方法 :(JQuery中90%都是方法,没有参数是获取,带参数是设置) $("#id").css('backgroundColor','blue'); .cs ...
- SQL Server高级内容之表表达式和复习
1. 表表达式 (1) 将表作为一个源或将查询的一个结果集作为一个源,对源做处理,然后得到一个新的数据源,对其进行查询. (2)表表达式放在from子句中 (3)派生表,将表的查询得到的结果集作为一 ...