Container With Most Water
You get the heights of walls that stand in a row, one step apart. Pick two of them so that the most water fits between them: the distance between the walls times the lower one. Our example: heights [2, 6, 3, 5, 1, 4, 7, 3] — the answer is 30, the walls 6 and 7.
- input
height: the heights of vertical lines standing one unit apart.- returns
- The most water a container of two of the lines and the ground can hold: the largest
(j − i) · min(height[i], height[j]). - constraints
- 2 ≤ height.length ≤ 3000
- 0 ≤ height[i] ≤ 10⁴
- examples
- Open a test in the tests panel: it shows the input and the expected answer.
/**
* @param {number[]} height
* @return {number}
*/
function maxArea(height) {
}
Ctrl ↵
format
reset
saved in this browser
Stuck?
See the solution run step by step on our example, then why it works and its complexity.
Next: more two pointers problems
- 125Valid PalindromeEasysolve
- 26Remove Duplicates from Sorted ArrayEasysolve
- 283Move ZeroesEasysolve
- 88Merge Sorted ArrayEasysolve
- 27Remove ElementEasysolve
- 392Is SubsequenceEasysolve
- 167Two Sum II - Input Array Is SortedMediumsolve
- 153SumMediumsolve
- 80Remove Duplicates from Sorted Array IIMediumsolve
- 189Rotate ArrayMediumsolve
- 151Reverse Words in a StringMediumsolve
- 5Longest Palindromic SubstringMediumsolve
- 42Trapping Rain WaterHardsolve
The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.