#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 题目名称 回文数 题目摘要 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 思路 一些一定不为回文数的 ...
随机推荐
- Making the impossible: 3 nodes intercontinental replication--转载
原文:http://www.percona.com/blog/2012/01/11/making-the-impossible-3-nodes-intercontinental-replication ...
- MySQL之事务隔离级别--转载
转自:http://793404905.blog.51cto.com/6179428/1615550 本文通过实例展示MySQL事务的四种隔离级别. 1 概念阐述 1)Read Uncommitted ...
- 安装opencms时遇到问题及解决方法
1. MySQL system variable 'max_allowed_packet' http://blog.csdn.net/hqa_ii/article/details/6872367 安装 ...
- Socket异步通信学习二
接下来是服务器部分,采用异步模式,新建了一个AsynServer类,用于存放socket服务器代码,主要有4个方法: 有一个全局socket,下面四个方法中都用到. Socket socket = n ...
- CSS选择器优先级
1.类的覆盖顺序和应用的时候引用的顺序没关系,覆盖顺序取决于类定义的顺序 例如: .a{ color:red } .b{ color:green } 由于b晚于a定义,所以b覆盖a,反 ...
- C语言进行CGI程序设计
一.CGI概述 CGI(公用网关接口)规定了Web服务器调用其他可执行程序(CGI程序)的接口协议标准.Web服务器通过调用CGI程序实现和Web浏览器的交互, 也就是CGI程序接受Web浏览器发送给 ...
- dede版权信息修改
login:dede-templets-login.htm 系统主页:dede-templets-index2.htm 主体内容在index_body.htm文件 干掉: $(function() ...
- 【转】PL/SQL编辑数据"这些查询结果不可更新,请包括ROWID或使用SELECT...FOR UPDATE获得可更新结果"处理
[转]PL/SQL编辑数据"这些查询结果不可更新,请包括ROWID或使用SELECT...FOR UPDATE获得可更新结果"处理 只要有人用了: select t.* from ...
- coding
public class zengzhiwei { public static void main(String args[]) { int i; System.out.println("h ...
- HTTP和HTTPS详解
http://blog.csdn.net/mingli198611/article/details/8055261/ 转自:http://www.cnblogs.com/ok-lanyan/archi ...