【leetcode】Container With Most Water
2015-02-06 14:08
344 查看
题目描述:
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.Note: You may not slant the container.
解题思路:
这道题说白了就是选两个值中较小的一个\(min(a_i,a_j)\),然后乘以他们之间的间距\((i-j)\)的最大值.最简单的\(O(n^2)\)的遍历。但是我们可以采取贪心的算法,从两端开始不断选择较大的一侧组成新的container。class Solution: # @return an integer def maxArea(self, height): res = 0 l = len(height) i = 0 j = l-1 while i < j: t = min(height[i], height[j]) * (j-i) if t > res: res = t if height[i] < height[j]: i += 1 else: j -= 1 return res
相关文章推荐
- 【Leetcode】Container With Most Water
- leetcode Container With Most Water
- [LeetCode]Container With Most Water, 解题报告
- LeetCode题解——Container With Most Water
- leetcode 11 -- Container With Most Water
- [LeetCode] Container With Most Water
- leetcode:Container With Most Water
- LeetCode 之 Container With Most Water — C 实现
- Leetcode -- Python --Container With Most Water
- [leetcode]Container With Most Water(using Python)
- leetcode---container-with-most-water---数组
- [C++]LeetCode: 39 Container With Most Water
- [LeetCode] Container With Most Water 解题报告
- LeetCode: Container With Most Water [Java]
- LeetCode-Container With Most Water
- leetcode Container With Most Water
- LeetCode Container With Most Water
- LeetCode No.11 Container With Most Water
- LeetCode解题报告—— Container With Most Water & 3Sum Closest & Letter Combinations of a Phone Number
- LeetCode 11 Container With Most Water