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的更多相关文章

  1. Leetcode Array 11 Container With Most Water

    题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...

  2. 【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).  ...

  3. leetcode个人题解——#11 Container with most water

    class Solution { public: int maxArea(vector<int>& height) { ; ; ; while(l < r) { int h ...

  4. 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 用双指针向中间滑动,较小的高度就作为当前情 ...

  5. LeetCode Array Medium 11. Container With Most Water

    Description Given n non-negative integers a1, a2, ..., an , where each represents a point at coordin ...

  6. Leetcode 11. Container With Most Water(逼近法)

    11. Container With Most Water Medium Given n non-negative integers a1, a2, ..., an , where each repr ...

  7. 刷题11. Container With Most Water

    一.题目说明 11.Container With Most Water,这个题目难度是Medium. 二.我的做法 乍一看,简单啊,两个for循环就可以了,我在本地写的. #include<io ...

  8. 如何装最多的水? — leetcode 11. Container With Most Water

    炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...

  9. 《LeetBook》leetcode题解(11):Container With Most Water[M] ——用两个指针在数组内移动

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  10. [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: 找出数组中最大的数 ...

随机推荐

  1. python 实现服务树结构化

    1.  所有服务树数据 tree_list = [{'id': 1, 'pid': 0, 'name': '1211', 'path': '1211', 'leaf': 0, 'type': 0}, ...

  2. PostMan 中使用Datas 文件一次Run 只能使用一个

    PostMan 一次run  只能有一个文件. Note that you can only use one data file for one run.

  3. JS批量绑定事件

    ,,,,] for(var j in a){ $("#" + j).click(function () { // 前提是先动态生成id是j的标签 var id_cm = $(thi ...

  4. 使用tomcat-maven-plugin自动化部署应用

    目标:将应用自动打包并自动部署到远程tomcat容器. 1.在pom.xml中添加plugin <plugin> <groupId>org.apache.tomcat.mave ...

  5. POJ 1741 单次询问树上距离<=K的点对数 点分治

    #include<cstdio> #include<cstring> #include<algorithm> using namespace std; ; ; ], ...

  6. Decrator 装饰模式

    注:装饰模式是结构型设计模式,动态地给一个对象增加一些额外的职责,就增加的功能来说,Decorator模式相比生成子类更加灵活. 主要是利用组合代替继承来实现业务功能,对动态改变业务实现来说,相比更灵 ...

  7. Mybatis映射文件中数据库表列名称和POJO成员域的联系

    下面是Mybatis的SQL映射文件. <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE ma ...

  8. TCP_Wrappers访问控制

    一.TCP_Wrappers简介 对有状态连接的特定服务进行安全检测并实现访问控制,它以库文件形式实现,某进程是否接受libwrap的控制取决于发起此进程的程序在编译时是否针对libwrap进行编译的 ...

  9. ValueError: Only call `softmax_cross_entropy_with_logits` with named arguments (labels=..., logits=..., ...)

    tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=predicton,labels=y))

  10. 帝都之行9day:正式上班第一天

    今天是我正式上班的第一天. 面了两天,三家公司,然后周五就去办入职了,我是不是太随便了点,捂脸. 不管怎么说,又要开始上班啦,CRUD的日子又要开始了…… 加油吧!