11. Container With Most Water
Container With Most Water (LeetCode 11) is a Medium Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Two Pointers pattern, in the Converging (Sorted Array Target Sum) subpattern.
- Blind 75 · #11 of 75 · Two Pointers
- NeetCode 150 · #13 of 150 · Two Pointers
- NeetCode 250 · #33 of 250 · Two Pointers
How to solve Container With Most Water
Container With Most Water is a Converging Two Pointers problem, part of Two Pointers. Learn converging two pointers for sorted arrays. Solve two sum, three sum, and container problems efficiently.
Work towards the solution in these steps:
- If you simulate the problem, it will be O(n^2) which is not efficient.
- Try to use two-pointers. Set one pointer to the left and one to the right of the array. Always move the pointer that points to the lower line.
- How can you calculate the amount of water at each step?
The reference solution runs in O(n) time and O(1) space.
The Container With Most Water solution and editorial walks through the full approach with reference code.
Problem statement
You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]).
Find two lines that together with the x-axis form a container, such that the container contains the most water.
Return the maximum amount of water a container can store.
Notice that you may not slant the container.
Example 1:
Input: height = [1,8,6,2,5,4,8,3,7] Output: 49 Explanation: 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 2:
Input: height = [1,1] Output: 1
Constraints:
- n == height.length
- 2 <= n <= 10^5
- 0 <= height[i] <= 10^4
Topics and companies
Topics: Array, Two Pointers, Greedy.
Reported in interviews at Amazon, Google, Microsoft, Facebook, Goldman Sachs, Adobe, Apple.
How to practise Container With Most Water on Thita.ai
Starter code is provided in C, C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(n) time and O(1) space.
Open Container With Most Water in the code editor, or read the Container With Most Water editorial for a worked solution with its approach and complexity analysis.
Where Container With Most Water sits in the DSA pattern sheet
- Two Pointers pattern guide
- Converging (Sorted Array Target Sum) subpattern
- The full DSA patterns sheet
Problems related to Container With Most Water
Other problems that use the same Converging (Sorted Array Target Sum) and Two Pointers techniques:
- Boats to Save People — Medium, same subpattern
- 4Sum — Medium, same subpattern
- Squares of a Sorted Array — Easy, same subpattern
- 3Sum Closest — Medium, same subpattern
- 3Sum Smaller — Medium, same subpattern
- 3Sum — Medium, same subpattern
Container With Most Water FAQ
What is the LeetCode number of Container With Most Water?
Container With Most Water is LeetCode problem 11, rated Medium.
What pattern is used to solve Container With Most Water?
Container With Most Water is a Converging Two Pointers problem. It belongs to the Two Pointers pattern, in the Converging (Sorted Array Target Sum) subpattern.
What is the time complexity of Container With Most Water?
The reference solution runs in O(n) time and O(1) space.
Which companies ask Container With Most Water?
Container With Most Water has been reported in interviews at Amazon, Google, Microsoft, Facebook, Goldman Sachs, Adobe, Apple.