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: 找出数组中最大的数 ...
随机推荐
- Centos搭建hexo教程
hexo文档:https://hexo.io/zh-cn/ 1.安装Git # sudo yum install git-core// 查看版本# git version// 输出git versio ...
- php--常见算法3
<?php function leijia($number){ $arr=[]; for($i=1;$i<=$number;$i++) { for($j=1;$j<=$number; ...
- Java学习笔记【五、字符串】
String类 11种构造,不一一列举 常用方法 s.length() 返回字符串长度 s1.contact(s2) 连接s1.s2 String.format("aaa %f bbb %d ...
- /etc/ld.so.conf.d/目录下文件的作用
在了解/etc/ld.so.conf.d/目录下文件的作用之前,先介绍下程序运行是加载动态库的几种方法: 第一种,通过ldconfig命令 ldconfig命令的用途, 主要是在默认搜寻目录( ...
- IPC之mqueue.c源码解读
队列的意思应该大家都清楚,不过还有有一些细节的地方不知道,下面是一个队列的源码,因该说这是队列的一部分,不是全部.而且是linux中队列,其他各种OS中队列大同小异. /* * POSIX messa ...
- java8学习之Optional深入详解
自上次[http://www.cnblogs.com/webor2006/p/8243874.html]函数式接口的学习告一段落之后,这次来学习一下Optional,它并非是函数式接口的概念,点击查看 ...
- 实际应用脚本备份1——Ubuntu下应用升级脚本与执行方法
程序自动更新脚本,命名为makefile: build:run run: killall java /webapps/‘应用目录名’/ /webapps/ ‘应用目录名’/ cd /opt/apach ...
- python中的apply(),applymap(),map() 的用法和区别
平时在处理df series格式的时候并没有注意 map和apply的差异 总感觉没啥却别.不过还是有区别的.下面总结一下: import pandas as pd df1= pd.DataFrame ...
- php类相关知识---__unset和__isset
__unset 删除非公有属性,在外部调用unset时发生, __isset用来检测对象属性是否设置值 <?php class coach { protected $chairfit = &q ...
- 开始学习shell
运行shell脚本有两种方法: 作为可执行程序,假如在某个目录下,编写了一个shell脚本test.sh,想要执行这个脚本,就需要先cd进入脚本所在目录, chmod +x ./test.sh # 是 ...