[LeetCode]题解(python):011-Container With Most Water
题目来源:
https://leetcode.com/problems/container-with-most-water/
题意分析:
给出一个n长度的非0数组,a1,a2,……,an,ai代表在坐标i上的高度为ai。以以ai,aj为高,i到j为底,可以构造出一个容器。那么求出这些容器中可以装的水的最大容积(容器不能倾斜)。例如数组[2,1],一共可以构造1个容器,这个容器的四个端点坐标是(0,0),(0,2),(1,1),(1,1),那么他可以装的最大的水容积是(1-0)*1 = 1.
题目思路:
我们不难发现,水容积的大小是由短高度决定的。暴力的方法就是把所有的容器找出来,算出他们的水容积,一一比较,然后得到最大值,这种方法的时间复杂度是(O(n^2))。很明显会TLE。
我们认真研究一下寻找过程,我们从第一个高度为起始容器壁,那么我们直接以最后一个高度为终止壁,如果a1 <= an,那么以a1为起始的容器最大是a1 * (n - 1),以a1为容器壁的最大容器计算出来的。那么以a1为壁的所有情况不需要再考虑,接着考虑a2的;同理,如果a1 > an,an不再考虑,考虑an-1,这有点类似"夹逼定理"。比较ai和aj(i<j)如果ai <= aj,i++;否者j ++直到i == j。这个算法的时间复杂度是(O(n))。
代码(python):
class Solution(object):
def maxArea(self, height):
"""
:type height: List[int]
:rtype: int
"""
size = len(height) # the size of height
maxm = 0 # record the most water
j = 0
k = size - 1
while j < k:
if height[j] <= height[k]:
maxm = max(maxm,height[j] * (k - j))
j += 1
else:
maxm = max(maxm,height[k] * (k - j))
k -= 1
return maxm
转载请注明出处:http://www.cnblogs.com/chruny/p/4817787.html
[LeetCode]题解(python):011-Container With Most Water的更多相关文章
- No.011 Container With Most Water
11. Container With Most Water Total Accepted: 86363 Total Submissions: 244589 Difficulty: Medium Giv ...
- LeetCode--No.011 Container With Most Water
11. Container With Most Water Total Accepted: 86363 Total Submissions: 244589 Difficulty: Medium Giv ...
- 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】011 Container With Most Water
题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...
- 【JAVA、C++】LeetCode 011 Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- [Leetcode]011. Container With Most Water
public class Solution { public int maxArea(int[] height) { int left = 0, right = height.length - 1; ...
- leetcode第11题--Container With Most Water
Problem: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate ...
- 【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 笔记系列二 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
题目 Given n non-negative integers a1, a2, -, an, where each represents a point at coordinate (i, ai). ...
随机推荐
- Html 小插件7 新闻免费代码
http://rss.cnfol.com/freecode/ 定制方法图:
- Openstack命令收集
查看rabbitmq 队列 rabbitmqctl list_queues 查看keystone的用户 keystone user-list 查看keystone endpoint keystone ...
- VM Agent 和扩展程序
VM Agent 和扩展程序 - 第 1 部分 Windows Azure基础结构服务最近宣布了一项新功能VM Agent.VMAgent是一个轻量级进程,用于启动由Microsoft或合作伙伴 ...
- 《Java4Android视频教程》学习笔记(二)
一:面向对象 1.对象 ①对象的使用方法 对象.变量 对象.方法 ②匿名对象 new A().方法 new A().变量 匿名对象会被分配到对内存中 java内存处理机制会对一定时间内无指针指向的对象 ...
- 【Perl学习笔记】2. perl中的bless理解
bless有两个参数:对象的引用.类的名称. 类的名称是一个字符串,代表了类的类型信息,这是理解bless的关键. 所谓bless就是把 类型信息 赋予 实例变量. 程序包括5个文件:person.p ...
- cluster maintain manager Software群集管理软件
1,ocfs2 2,crmsh(cluster management shell,)==crm shell [pacemaker OpenAIS,heartbeat,corosync,crmsh] 3 ...
- Finding the Longest Palindromic Substring in Linear Time
Finding the Longest Palindromic Substring in Linear Time Finding the Longest Palindromic Substring i ...
- c#打印记忆功能
下面这些实例都可以拷下直接用 总体思路:保存打印设置信息到本地文件,下次打印的时候直接读取文件信息,通过序列化与反序列化来获取值.本例只是针对打印的横纵向进行设置,读者也可以增加其他设置信息进行保存读 ...
- Android中各种Adapter的使用方法
1.概念 Adapter是连接后端数据和前端显示的适配器接口.是数据和UI(View)之间一个重要的纽带.在常见的View(ListView,GridView)等地方都须要用到Adapter.例如以下 ...
- Android仿人人客户端(v5.7.1)——个人主页(三)
转载请标明出处:http://blog.csdn.net/android_ls/article/details/9405089 声明:仿人人项目,所用所有图片资源都来源于其它Android移动应用,编 ...