comments: true difficulty: Medium
tags: - Greedy - Array - Two Pointers
You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the iᵗʰ 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.
Note: 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.length2 <= n <= 10⁵0 <= height[i] <= 10⁴
We use two pointers l and r initialized to the beginning and end of the array. The goal is to find the maximum area formed between any two lines. The area is calculated using:
area = min(height[l], height[r]) * (r - l)
We continuously move the pointer pointing to the shorter line inward, in hopes of finding a taller line and increasing the area.
Time Complexity: O(n)
Space Complexity: O(1)
class Solution:
def maxArea(self, height: List[int]) -> int:
l, r = 0, len(height) - 1
ans = 0
while l < r:
t = min(height[l], height[r]) * (r - l)
ans = max(ans, t)
if height[l] < height[r]:
l += 1
else:
r -= 1
return ansclass Solution {
public int maxArea(int[] height) {
int l = 0, r = height.length - 1;
int ans = 0;
while (l < r) {
int t = Math.min(height[l], height[r]) * (r - l);
ans = Math.max(ans, t);
if (height[l] < height[r]) {
++l;
} else {
--r;
}
}
return ans;
}
}class Solution {
public:
int maxArea(vector<int>& height) {
int l = 0, r = height.size() - 1;
int ans = 0;
while (l < r) {
int t = min(height[l], height[r]) * (r - l);
ans = max(ans, t);
if (height[l] < height[r]) {
++l;
} else {
--r;
}
}
return ans;
}
};var maxArea = function (height) {
let [l, r] = [0, height.length - 1];
let ans = 0;
while (l < r) {
const t = Math.min(height[l], height[r]) * (r - l);
ans = Math.max(ans, t);
if (height[l] < height[r]) {
++l;
} else {
--r;
}
}
return ans;
};function maxArea(height: number[]): number {
let [l, r] = [0, height.length - 1];
let ans = 0;
while (l < r) {
const t = Math.min(height[l], height[r]) * (r - l);
ans = Math.max(ans, t);
if (height[l] < height[r]) {
++l;
} else {
--r;
}
}
return ans;
}func maxArea(height []int) int {
l, r := 0, len(height)-1
ans := 0
for l < r {
t := min(height[l], height[r]) * (r - l)
if t > ans {
ans = t
}
if height[l] < height[r] {
l++
} else {
r--
}
}
return ans
}public class Solution {
public int MaxArea(int[] height) {
int l = 0, r = height.Length - 1;
int ans = 0;
while (l < r) {
int t = Math.Min(height[l], height[r]) * (r - l);
ans = Math.Max(ans, t);
if (height[l] < height[r]) {
++l;
} else {
--r;
}
}
return ans;
}
}class Solution {
function maxArea($height) {
$l = 0;
$r = count($height) - 1;
$ans = 0;
while ($l < $r) {
$t = min($height[$l], $height[$r]) * ($r - $l);
$ans = max($ans, $t);
if ($height[$l] < $height[$r]) {
++$l;
} else {
--$r;
}
}
return $ans;
}
}impl Solution {
pub fn max_area(height: Vec<i32>) -> i32 {
let mut l = 0;
let mut r = height.len() - 1;
let mut ans = 0;
while l < r {
ans = ans.max(height[l].min(height[r]) * ((r - l) as i32));
if height[l] < height[r] {
l += 1;
} else {
r -= 1;
}
}
ans
}
}