LeetCode---Container With Most Water(11)
Description: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.
问题:对于非负整数数组:[a1,a2,a3,…,an],点(i,0)(i,ai)组成线段i,点(j,0)(j,aj)组成线段j,这两条线段与x轴组成一个容器,求解使得容器可容纳水最多时的截面面积Area=|i-j|*min(ai,aj)。
以下描述中假定i<j
解法1(暴力解法):
拿到题目首先会想到使用暴力解法,即算出所有线段之间可能组成的面积,再找到其中的最大面积。
Python:
class Solution(object):
def maxArea(self, height):
length = len(height)
MaxArea = 0
for i in range(length):
j = i+1
while(j<length):
MaxArea = max(MaxArea, (j - i)*min(height[i], height[j]))
j = j+1
return MaxArea
当提交代码后会提示超时。显而易见当数组height很大时,使用该方法需要计算出所有结果,算法时间复杂度为:O(n^2)

思考一下:如何减少计算次数,使算法复杂度为O(n)?即能否只遍历一次数组就能找到最大值?对于面积Area=(j-i)*min(ai,aj),假设min(ai,aj)=ai时,当(j-i)越大Area值才会越大。即应选择离ai最远的大于ai的元素。
解法2:
参考上述思路,设置两个指针i,j指向数组最左端与最右端,当最左端值小于最右端时,保存面积Area = (j-i)*min(height[i],height[j]),接着指针i向右移动,继续比较。当最右端小于最左端时,保存面积,接着指针j向左移动,继续比较。
Python:
class Solution(object):
def maxArea(self, height):
i, j = 0, len(height) - 1
MaxArea = 0
while (i < j):
MaxArea = max(MaxArea, (j - i)*min(height[i], height[j]))
if height[i] < height[j]:
i=i+1
else:
j=j-1
return MaxArea
LeetCode---Container With Most Water(11)的更多相关文章
- LeetCode:Container With Most Water,Trapping Rain Water
Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...
- [LeetCode] Container With Most Water 装最多水的容器
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- [LeetCode]Container With Most Water, 解题报告
前言 难怪LeetCode OJ在找工作时被很多人推荐,发现了这道最大蓄水题目就是美团的笔试最后一道题,当时我霸笔只有着一道题目没有答出来,因此也就没有获得面试机会,可惜了 题目 Given n no ...
- C++LeetCode:: Container With Most Water
本来写的题目不是这个,而是字符串匹配,考虑了很多情况写了很久最后看了solution,发现可以用动态规划做.感觉被打击到了,果断先放着重新写一个题,后面心情好了再重新写吧,难过.每天都要被LeetCo ...
- leetcode Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- [LeetCode] Container With Most Water 简要分析
前言 这题非要说贪心的话也算是吧,不过最主要的特征还是双指针.LC的题好像不少都是扔倆头尾指针然后遍历一遍完事儿的.这道题倒是“短板效应”的不错体现了. 题目 题目链接 Given n non-neg ...
- [Leetcode] Container With Most Water ( C++)
题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...
- LeetCode——Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- LeetCode Container With Most Water (Two Pointers)
题意 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai ...
- [Leetcode] container with most water 最大水容器
Given n non-negative integers a1 , a2 , ..., an , where each represents a point at coordinate (i, ai ...
随机推荐
- PortableApps使用入门
PortableApps使用入门 Software 介绍 添加软件 绿软下载站推荐 介绍 官网:http://portableapps.com/ PortableApps作为一款卓越的绿软管理软件,它 ...
- Linux性能分析工具top命令详解
top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,常用于服务端性能分析. top命令说明 [www.linuxidc.com@linuxidc-t-tomcat-1 ...
- Java sax、dom、pull解析xml
-------------------------------------SAX解析xml---------------------------------- >Sax定义 SAX是一个解析速度 ...
- 配置Tomcat线程参数maxThreads、acceptCount
一.配置Tomcat/conf/server.xml修改配置 <Connector port="8080" protocol="org.apache.coyote. ...
- 【BZOJ2729】【HNOI2012】排队(组合数学)
不想弄题面了... 题解 做这道题目我真的好蠢... 好容易的数学题目 很明显自己写高精度吧...(不解释了) 剩下的如何计算. 要有两类情况 ①老师之间有男生 那么,这种情况下,直接插空就行了 先把 ...
- 【CJOJ2499】【DP合集】棋盘 chess
Description 给出一张 n × n 的棋盘,格子有黑有白.现在要在棋盘上放棋子,要求: • 黑格子上不能有棋子 • 每行每列至多只有一枚棋子 你的任务是求出有多少种合法的摆放方案.答案模 1 ...
- CDQ 分治算法模板
CDQ分治 1.三维偏序问题:三维偏序(陌上花开) #include<bits/stdc++.h> #define RG register #define IL inline #defin ...
- [BZOJ1861][ZJOI2006]书架
BZOJ Luogu Description 小T有一个很大的书柜.这个书柜的构造有些独特,即书柜里的书是从上至下堆放成一列.她用1到n的正整数给每本书都编了号. 小T在看书的时候,每次取出一本书,看 ...
- Nginx负载均衡——基础功能
熟悉Nginx的小伙伴都知道,Nginx是一个非常好的负载均衡器.除了用的非常普遍的Http负载均衡,Nginx还可以实现Email,FastCGI的负载均衡,甚至可以支持基于Tcp/UDP协议的各种 ...
- Nginx负载均衡——扩展功能(NGINX Plus)
本文主要是介绍了NGINX Plus的相关功能,横跨了NGINX Plus R5/R6/R7/R9等各个不同版本的更新. 什么是NGINX Plus? 顾名思义,就是Nginx的加强版或者扩展版.我们 ...