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: 找出数组中最大的数 ...
随机推荐
- http://www.moext.com博客搬家到这里啦
1.原博客莫叉特用的是自己的域名http://www.moext.com,由于服务器在国外,访问不太稳定,SEO做得也很不好: 2.喜欢博客园的极简风格,目前来看广告量也在可接受范围: 3.一个偶然的 ...
- 使用HandyJSON导致的内存泄漏问题相关解决方法
在移动开发中,与服务器打交道是不可避免的,从服务器拿到的接口数据最终都会被我们解析成模型,现在比较常见的数据传输格式是json格式,对json格式的解析可以使用原生的解析方式,也可以使用第三方的,我们 ...
- 经典i++和++i问题(附带运算符优先级问题)
转自 https://blog.csdn.net/mustard1020/article/details/79617865 1.i++和++i的区别 (1)i++简单来说就是先用i的值来参加表 ...
- Windows环境中编译opencv3.0同时加入OPENCV_contrib库及解决遇到相关问题[contrib 必须要3.1以上的opencv才支持了]
更新:现在contrib库必须要opencv3.1以上才能支持编译通过了. 方法和步骤还是和本篇文章一样. ############################################## ...
- zencart批量评论插件Easy Populate CSV add reviews使用教程
此插件在Easy Populate CSV 1.2.5.7b产品批量插件基础上开发,有1.3x与1.5x两个版本. zencart批量评论插件Easy Populate CSV add reviews ...
- Solr——Java应用
Solr有一个客户端SolrJ 创建一个Java Project 引入Jar包 添加test类 package com.solr.test; import java.io.IOException; i ...
- AVL树的介绍和实现
一.AVL树 AVL树是一种自平衡二叉查找树,因此在了解AVL树之前先介绍一下平衡二叉树.所谓平衡二叉树即该树中的任一个节点的左子树和右子树高度差不会超过1.如下图左是平衡二叉树,而右图则不是.节点4 ...
- 基于Hexo的个人博客搭建(上)
没有废话,直接开始. 1. 环境配置 —1.1 node.js安装 https://nodejs.org/en/download/ 下载最新版本即可,然后无脑安装(除了选安装目录的时候),为了保证安装 ...
- 什么是http协议(一)
http协议是大家在互联网中最为熟悉的协议,只要上网大家都会遇到,但是,很多人被问道什么是http协议,http协议的内容是什么就懵了.这里,我们随便聊聊http协议. 首先,我们说说协议.我一直觉得 ...
- vue环境搭建及简单接触
1.安装node环境 首先官网安装nodejs,下载地址https://nodejs.org/en/ 很多情况下,npm i 命令安装的包都是要科学上网的,或者就是国际网,下载速度很慢,不过有个淘宝镜 ...