Neetcode: Container with Most Water


Instructions

You are given an integer array heights where heights[i] represents the height of the ith bar.

You may choose any two bars to form a container. Return the maximum amount of water a container can store.

Example 1:

Graph representation of input array with shaded in area representing answer.
Input: height = [1,7,2,5,4,7,3,6]

Output: 36

Example 2:

Input: height = [2,2,2]

Output: 4

Constraints:

  • 2 <= height.length <= 1000
  • 0 <= height[i] <= 1000

Solutions

Python

class Solution:
    def maxArea(self, heights: List[int]) -> int:
        result_area = 0

        i = 0
        j = len(heights) - 1

        while i < j:
            result_area = max(result_area, self.calc_area(heights, i, j))

            if heights[i] < heights[j]:
                i += 1
            else:
                j -= 1

        return result_area

    def calc_area(self, heights: List[int], i: int, j: int) -> int:
        return (j - i) * min(heights[i], heights[j])
Made with Gatsby G Logo