LeetCode: Container With Most Water Posted on November 13, 2017July 26, 2020 by braindenny Get the most water from containers Similar Problems: Series: Trapping Rain & Follow-up CheatSheet: Leetcode For Code Interview CheatSheet: Common Code Problems & Follow-ups Tag: #trappingrain, #twopointer 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. The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49. Example: Input: [1,8,6,2,5,4,8,3,7] Output: 49 Github: code.dennyzhang.com Credits To: leetcode.com Leave me comments, if you have better ways to solve. Solution: Two Pointer ## https://code.dennyzhang.com/container-with-most-water ## Basic Ideas: two pointer ## ## Two pointer: ## From left to right ## From right to left ## ## If height[left]<height[right], move right doesn't make sense ## Likewise for the other one ## ## Complexity: Time O(n), Space O(1) class Solution: def maxArea(self, height: List[int]) -> int: n = len(height) left, right = 0, n-1 res = 0 while left<right: res = max(res, min(height[left], height[right])*(right-left)) if height[left]<height[right]: left += 1 else: right -= 1 return res Solution: Two Pointer // https://code.dennyzhang.com/container-with-most-water // Basic Ideas: Two pointer // Start with l, r = 0, len(l)-1 // Then move the pointer of shorter value // Let's check the case that both pointers are the same the value. // We won't find a better solution with only one edge of left pointer or right pointer. // Thus we can move either pointer // Complexity: Time O(n), Space O(1) func maxArea(height []int) int { l, r := 0, len(height)-1 res, v := 0, 0 for l<r { if height[l] <= height[r] { v = height[l]*(r-l) l++ } else { v = height[r]*(r-l) r-- } if v>res { res = v } } return res } Solution: Two Pointer + Pruning // https://code.dennyzhang.com/container-with-most-water // Basic Ideas: Two pointer // Start with l, r = 0, len(l)-1 // Then move the pointer of shorter value // Let's check the case that both pointers are the same the value. // We won't find a better solution with only one edge of left pointer or right pointer. // Thus we can move either pointer // Complexity: Time O(n), Space O(1) func maxArea(height []int) int { l, r := 0, len(height)-1 res, v := 0, 0 for l<r { lmax, rmax := height[l], height[r] if height[l] <= height[r] { v = height[l]*(r-l) } else { v = height[r]*(r-l) } if v>res { res = v } if height[l] <= height[r] { for l<r && height[l]<=lmax { l++ } } else { for l<r && height[r]<=rmax { r-- } } } return res } Post Views: 4 Post navigation LeetCode: Bitwise AND of Numbers RangeLeetCode: Rotate Array Leave a Reply Cancel replyYour email address will not be published.Comment Name Email Website