[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):

 1 class Solution(object):
 2     def maxArea(self, height):
 3         """
 4         :type height: List[int]
 5         :rtype: int
 6         """
 7         size = len(height) # the size of height
 8         maxm = 0 # record the most water
 9         j = 0
10         k = size - 1
11         while j < k:
12             if height[j] <= height[k]:
13                 maxm = max(maxm,height[j] * (k - j))
14                 j += 1
15             else:
16                 maxm = max(maxm,height[k] * (k - j))
17                 k -= 1
18         return maxm



转载请注明出处:http://www.cnblogs.com/chruny/p/4817787.html

时间: 2024-10-06 17:18:49

[LeetCode]题解(python):011-Container With Most Water的相关文章

LeetCode 011 Container With Most Water

[题目] 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 con

[LeetCode] 011. Container With Most Water (Medium) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 011.Container_With_Most_Water (Medium) 链接: 题目:https://oj.leetcode.com/problems/container-with-most-water/ 代码(github):https://github.com/illuz/leetcode 题意: 给一些

【LeetCode】011 Container With Most Water

题目: 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 cont

Java for LeetCode 011 Container With Most Water

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 containe

leetcode第11题--Container With Most Water

Problem: 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

[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql)

全部最新的题解可以在 我的 github 上找,欢迎 star 和 watch ~ 更新中~~ 说明 这个系列的题解包括用 C++/Java/Python 写的 leetcode 上的算法题目,和 Sql 写的 leetcode 上的数据库题目. 有些题目虽然 AC 了却还没写分析,所以这次就开坑来完成. 链接: 我的 github Leetcode Algorithms Problems Leetcode Database Problems CSDN 题解索引 001.Two_Sum (Med

LeetCode: Container With Most Water 题解

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 containe

【leetcode刷提笔记】Container With Most Water

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 containe

[LeetCode]题解(python):031-Next Permutation

题目来源 https://leetcode.com/problems/next-permutation/ Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible

【LeetCode】Container With Most Water

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 containe