--- title: "#11 Container With Most Water" tags: LeetCode, Top100 --- #11 Container With Most Water == 題目描述 -- Given n non-negative integers ==a~1~, a~2~, …, a~n~== , where each represents a point at coordinate ==(i, a~i~)==. n vertical lines are drawn such that the two endpoints of line ==i== is at ==(i, a~i~)== 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. <!--more--> 解題思維 -- 利用兩點逼近,找出有可能的最大可能。 ## Two Pointer Approach ```python class Solution: def maxArea(self, height: List[int]) -> int: left, right = 0, len(height)-1 maxWater = 0 while left != right: wight = right - left thisWater = min(height[left], height[right])*wight maxWater = max(maxWater, thisWater) if(height[left] < height[right]): left += 1 else: right -= 1 return maxWater if __name__ == "__main__": height = [1, 8, 6, 2, 5, 4, 8, 3, 7] print(Solution().maxArea(height=height)) ```