leetcode-hard-array-11 Container With Most Water -NO
mycode time limited
class Solution(object):
def maxArea(self, height):
"""
:type height: List[int]
:rtype: int
"""
final = 0
for i in range(1,len(height)):
temp = []
for j in range(i):
h = min(height[j],height[i])
width = i - j
temp.append(h*width)
final = max(final,max(temp))
return final
参考:
把求水的容量转换成求面积
假设: 第i条和第j条(i < j)线和x轴围起来面积最大,那么最大面积为Sij = min(ai, aj) * (j - i);
那么:
在j的右边没有比他更高的线
在i的左边也没有比他更高的线
证明 :
反证法:
如果在j的右边存在比他高的线为第k第线, 那么Sik = min(ai, ak) *(k - i), 由于k > j,所以Sik > Sij,与Sij最大的条件矛盾。
同理可证在i的左边也没有比他更高的线。
从上面说的性质可以说明, 从头和尾分别向中间遍历a1, a2, ..., an, 如果遍历的线比前面的高,则更新当前最高,并算出面积与之前存的最大面积比较,当前更大则更新,否之则跳过。
但有一个问题: 头和尾哪个先向中间遍历呢?
--高度矮的个。 因为如果高的向中间移动了,那矮的那边就不可能遍历到高的现在遍历的那个点了,就可能找不到最大面积。
例如:

这也是动态规划的用法。
class Solution(object):
def maxArea(self, height):
"""
:type height: List[int]
:rtype: int
"""
l = 0
r = len(height)-1
res = 0
while l < r :
h = min(height[l] , height[r])
w = r - l
res = max(res , h*w)
#print(l,r,res)
if height[l] < height[r]:
l += 1
else:
r -= 1
return res
leetcode-hard-array-11 Container With Most Water -NO的更多相关文章
- Leetcode Array 11 Container With Most Water
题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...
- 【LeetCode two_pointer】11. Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- leetcode个人题解——#11 Container with most water
class Solution { public: int maxArea(vector<int>& height) { ; ; ; while(l < r) { int h ...
- leetcode 11. Container With Most Water 、42. Trapping Rain Water 、238. Product of Array Except Self 、407. Trapping Rain Water II
11. Container With Most Water https://www.cnblogs.com/grandyang/p/4455109.html 用双指针向中间滑动,较小的高度就作为当前情 ...
- LeetCode Array Medium 11. Container With Most Water
Description Given n non-negative integers a1, a2, ..., an , where each represents a point at coordin ...
- Leetcode 11. Container With Most Water(逼近法)
11. Container With Most Water Medium Given n non-negative integers a1, a2, ..., an , where each repr ...
- 刷题11. Container With Most Water
一.题目说明 11.Container With Most Water,这个题目难度是Medium. 二.我的做法 乍一看,简单啊,两个for循环就可以了,我在本地写的. #include<io ...
- 如何装最多的水? — leetcode 11. Container With Most Water
炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...
- 《LeetBook》leetcode题解(11):Container With Most Water[M] ——用两个指针在数组内移动
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- [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: 找出数组中最大的数 ...
随机推荐
- MongoDB学习笔记,基础+增删改查+索引+聚合...
一 基础了解 对应关系 -> https://docs.mongodb.com/manual/reference/sql-comparison/ database -> database ...
- 制作CentOS8安装U盘时遇到的“Minimal BASH-like...”问题
---恢复内容开始--- CentOS8已经推出了,正好最近新到了块服务器硬盘需要安装系统,就拿过来尝一下鲜. 下载好iso文件后,以制作CentOS7安装盘相同的步骤,用UltroISO(软碟通)往 ...
- 开关灯 ToggleButton
开关灯 ToggleButton textOn:对应true的时候:textOff:对应false的时候:给toggleButton设置监听器toggleButton.setOnCheckChange ...
- Maximum Xor Secondary CodeForces - 281D (单调栈)
Bike loves looking for the second maximum element in the sequence. The second maximum element in the ...
- python和ruby:一些需要注意的小区别。
python和ruby的一些区别 基础区别 运算符号/和// ruby只有/符号.它根据操作的数字类型返回对应的结果.如果数字的类型是int,则返回整除结构,如果是float,则返回float类型的计 ...
- ubuntu NAT dhcp
说明: 1.在服务器版本中,没有想桌面版一样的NetworkManager工具,所以的一切都是在命令行上操作的. 2.本文只针对DHCP默认分配的IP进行查看. 方法: 1.如果要使用DHCP,那么需 ...
- 【原】GitHub使用教程
GitHub使用教程 1 注册 GitHub 账户 要想使用github第一步当然是注册github账号了(www.github.com). 2 安装客户端 msysgit github是服务端,要想 ...
- vue实例之组件开发:图片轮播组件
一.普通方式: 其中,index是关键. <!DOCTYPE html> <html lang="en"> <head> <meta ch ...
- jquery button选择器 语法
jquery button选择器 语法 作用::button 选择器选取类型为 button 的 <button> 元素和 <input> 元素.大理石平台价格表 语法:$(& ...
- 《剑指offer》算法题第八天
今日题目(对应书上第39~42题): 数组中出现次数超过一半的数字 最小的k个数(top k,重点!) 数据流中的中位数 连续子数组的最大和 今天的题目都比较经典,特别是第2题. 1. 数组中出现次数 ...