[勇者闯LeetCode] 11. Container With Most Water
2017-03-19 11:10
323 查看
[勇者闯LeetCode] 11. Container With Most Water
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.Note: You may not slant the container and n is at least 2.
Information
Tags: Array | Two PointersDifficulty: Medium
Solution
用两个指针从两端开始向中间靠拢,如果左边线段短于右边,那么左边指针右移,反之右边指针左移,直到左右指针移到中间重合,记录这个过程中组成木桶的最大容积。class Solution(object): def maxArea(self, height): """ :type height: List[int] :rtype: int """ l, r, ans = 0, len(height)-1, 0 while l < r: ans = max(ans, (r - l) * min(height[l], height[r])) if height[l] < height[r]: l += 1 else: r -= 1 return ans
相关文章推荐
- [勇者闯LeetCode] 108. Convert Sorted Array to Binary Search Tree
- [勇者闯LeetCode] 28. Implement strStr()
- [勇者闯LeetCode] 13. Roman to Integer
- [勇者闯LeetCode] 191. Number of 1 Bits
- [勇者闯LeetCode] 35. Search Insert Position
- [勇者闯LeetCode] 155. Min Stack
- [勇者闯LeetCode] 190. Reverse Bits
- [勇者闯LeetCode] 215. Kth Largest Element in an Array
- [勇者闯LeetCode] 110. Balanced Binary Tree
- [勇者闯LeetCode] 257. Binary Tree Paths
- [勇者闯LeetCode] 27. Remove Element
- [勇者闯LeetCode] 38. Count and Say
- [勇者闯LeetCode] 83. Remove Duplicates from Sorted List
- [勇者闯LeetCode] 88. Merge Sorted Array
- [勇者闯LeetCode] 160. Intersection of Two Linked Lists
- [勇者闯LeetCode] 100. Same Tree
- [勇者闯LeetCode] 6. ZigZag Conversion
- [勇者闯LeetCode] 101. Symmetric Tree
- [勇者闯LeetCode] 104. Maximum Depth of Binary Tree
- [勇者闯LeetCode] 111. Minimum Depth of Binary Tree