#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 题目名称 回文数 题目摘要 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 思路 一些一定不为回文数的 ...
随机推荐
- mysql 源码--xpchild
http://www.cnblogs.com/xpchild/p/3825309.html
- java_spring_List,Map,Properties,Set注入与遍历
package com.dao.bean.www; import java.util.List; import java.util.Map; import java.util.Properties; ...
- Using zend-navigation in your Album Module
Using zend-navigation in your Album Module In this tutorial we will use the zend-navigation componen ...
- Asp.Net 之 调用远程Web_Service
一.添加web service引用 1.右键 Web 项目 → “添加服务引用”: 2.右键已有的 App_WebReferences 文件夹 → “添加服务引用”: 二.引用远程web servic ...
- Spring3之MVC
模式-视图-控制器(MVC)是UI设计中常见的设计模式, 该模式区分应用程序中的模式.视图和控制器三个角色,消除了业务逻辑与UI的耦合.模式负责封装视图展示的应用数据.视图应该只显示数据,不包含任何业 ...
- word-wrap,word-break,text-wrap的区别
今晚看到了无双老师关于word-wrap,word-break区别的讲解 http://www.cnblogs.com/2050/archive/2012/08/10/2632256.html 受益 ...
- javascript 获取下一个节点
下一个节点: nextElementSibling 上一个节点 previousElementSibling <div> <select onchange="alert(t ...
- jquery uploadify修改上传的文件名和显示
如果觉得看文章太麻烦,可以直接看参考:http://stackoverflow.com/questions/7707687/jquery-uploadify-change-file-name-as-i ...
- html元素拖拽
html <div> <div class="money-input"> 定投金额 : <div class="input-rela&quo ...
- 移动端web学习总结
前言: 一直想做一个移动端的阶段性学习总结,但是工作太忙总是加班.现在总算可以抽出一点时间来写一写,把知道的都写下来,这样就算忘掉了,也能很快想起来,不要太机智啊,哈哈哈! 一.移动端页面常识 1.常 ...