Skip to content

Commit 5871d7e

Browse files
committed
11. Container With Most Water
```Solution.c int maxArea(int* height, int heightSize) { int left = 0, right = heightSize - 1; int maxArea = 0; while (left < right) { int width = right - left; int currentHeight = height[left] < height[right] ? height[left] : height[right]; int area = width * currentHeight; maxArea = maxArea > area ? maxArea : area; if (height[left] < height[right]) { left++; } else { right--; } } return maxArea; } ``` ```Solution.cpp class Solution { public: int maxArea(vector<int>& height) { int left = 0; int right = height.size() - 1; int max_water = 0; while (left < right) { max_water = max(min(height[left], height[right]) * (right - left), max_water); if (height[left] <= height[right]) { left++; } else { right--; } } return max_water; } }; ``` ```Solution.cs public class Solution { public int MaxArea(int[] height) { int left = 0, right = height.Length - 1; int maxArea = 0; while (left < right) { int width = right - left; int currentHeight = Math.Min(height[left], height[right]); maxArea = Math.Max(maxArea, width * currentHeight); if (height[left] < height[right]) { left++; } else { right--; } } return maxArea; } } ``` ```Solution.dart class Solution { int maxArea(List<int> height) { int left = 0, right = height.length - 1; int maxArea = 0; while (left < right) { int width = right - left; int currentHeight = height[left] < height[right] ? height[left] : height[right]; maxArea = maxArea > (width * currentHeight) ? maxArea : (width * currentHeight); if (height[left] < height[right]) { left++; } else { right--; } } return maxArea; } } ``` ```Solution.erl -spec max_area(Height :: [integer()]) -> integer(). max_area(Height) -> Tuple = list_to_tuple(Height), max_area(Tuple, 0, tuple_size(Tuple) - 1, 0). max_area(Tuple, Left, Right, MaxWater) when Left < Right -> Width = Right - Left, LeftHeight = element(Left + 1, Tuple), RightHeight = element(Right + 1, Tuple), Area = min(LeftHeight, RightHeight) * Width, NewMaxWater = max(MaxWater, Area), if LeftHeight < RightHeight -> max_area(Tuple, Left + 1, Right, NewMaxWater); true -> max_area(Tuple, Left, Right - 1, NewMaxWater) end; max_area(_Tuple, _Left, _Right, MaxWater) -> MaxWater. ``` ```Solution.ex defmodule Solution do @SPEC max_area(height :: [integer]) :: integer def max_area(height) do height = List.to_tuple(height) do_max_area(height, 0, tuple_size(height) - 1, 0) end defp do_max_area(_, l, r, max) when l >= r, do: max defp do_max_area(height, l, r, max) do height_l = elem(height, l) height_r = elem(height, r) max = max(max, min(height_l, height_r) * (r - l)) if height_l < height_r do do_max_area(height, l + 1, r, max) else do_max_area(height, l, r - 1, max) end end end ``` ```Solution.go func maxArea(height []int) int { left, right := 0, len(height)-1 maxArea := 0 for left < right { width := right - left currentHeight := height[left] if height[right] < currentHeight { currentHeight = height[right] } area := width * currentHeight if area > maxArea { maxArea = area } if height[left] < height[right] { left++ } else { right-- } } return maxArea } ``` ```Solution.java class Solution { public int maxArea(int[] height) { int left = 0, right = height.length - 1; int maxArea = 0; while (left < right) { int width = right - left; int currentHeight = Math.min(height[left], height[right]); maxArea = Math.max(maxArea, width * currentHeight); if (height[left] < height[right]) { left++; } else { right--; } } return maxArea; } } ``` ```Solution.js /** * @param {number[]} height * @return {number} */ var maxArea = function(height) { let left = 0, right = height.length - 1; let maxArea = 0; while (left < right) { const width = right - left; const currentHeight = Math.min(height[left], height[right]); maxArea = Math.max(maxArea, width * currentHeight); if (height[left] < height[right]) { left++; } else { right--; } } return maxArea; }; ``` ```Solution.kt class Solution { fun maxArea(height: IntArray): Int { var left = 0 var right = height.size - 1 var maxArea = 0 while (left < right) { val width = right - left val currentHeight = minOf(height[left], height[right]) maxArea = maxOf(maxArea, width * currentHeight) if (height[left] < height[right]) { left++ } else { right-- } } return maxArea } } ``` ```Solution.php class Solution { /** * @param Integer[] $height * @return Integer */ function maxArea($height) { $left = 0; $right = count($height) - 1; $maxArea = 0; while ($left < $right) { $width = $right - $left; $currentHeight = min($height[$left], $height[$right]); $maxArea = max($maxArea, $width * $currentHeight); if ($height[$left] < $height[$right]) { $left++; } else { $right--; } } return $maxArea; } } ``` ```Solution.py class Solution: def maxArea(self, height: List[int]) -> int: left, right = 0, len(height) - 1 max_area = 0 while left < right: width = right - left current_height = min(height[left], height[right]) max_area = max(max_area, width * current_height) if height[left] < height[right]: left += 1 else: right -= 1 return max_area ``` ```Solution.rb # @param {Integer[]} height # @return {Integer} def max_area(height) return 0 if height.nil? || height.length < 2 left, right = 0, height.length - 1 max_area = 0 while left < right width = right - left current_height = [height[left], height[right]].min max_area = [max_area, width * current_height].max if height[left] < height[right] left += 1 else right -= 1 end end max_area end ``` ```Solution.rkt (define (max-area height) (define ht (list->vector height)) (define (loop left right max-area) (if (>= left right) max-area (let* ([left-height (vector-ref ht left)] [right-height (vector-ref ht right)] [width (- right left)] [area (* (min left-height right-height) width)] [new-max (max max-area area)]) (if (< left-height right-height) (loop (add1 left) right new-max) (loop left (sub1 right) new-max))))) (loop 0 (sub1 (vector-length ht)) 0)) ``` ```Solution.rs impl Solution { pub fn max_area(height: Vec<i32>) -> i32 { let mut left = 0; let mut right = height.len() - 1; let mut max_area = 0; while left < right { let width = (right - left) as i32; let current_height = height[left].min(height[right]); max_area = max_area.max(width * current_height); if height[left] < height[right] { left += 1; } else { right -= 1; } } max_area } } ``` ```Solution.scala object Solution { def maxArea(height: Array[Int]): Int = { var left = 0 var right = height.length - 1 var maxArea = 0 while (left < right) { val width = right - left val currentHeight = math.min(height(left), height(right)) maxArea = math.max(maxArea, width * currentHeight) if (height(left) < height(right)) { left += 1 } else { right -= 1 } } maxArea } } ``` ```Solution.swift class Solution { func maxArea(_ height: [Int]) -> Int { var left = 0, right = height.count - 1 var maxArea = 0 while left < right { let width = right - left let currentHeight = min(height[left], height[right]) maxArea = max(maxArea, width * currentHeight) if height[left] < height[right] { left += 1 } else { right -= 1 } } return maxArea } } ``` ```Solution.ts function maxArea(height: number[]): number { let left: number = 0, right: number = height.length - 1; let maxArea: number = 0; while (left < right) { const width: number = right - left; const currentHeight: number = Math.min(height[left], height[right]); maxArea = Math.max(maxArea, width * currentHeight); if (height[left] < height[right]) { left++; } else { right--; } } return maxArea; } ```
1 parent 14a06f3 commit 5871d7e

20 files changed

+341
-0
lines changed

res/ino/477955856.jpg

17.9 KB
Loading
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# [**11. Container With Most Water**](https://leetcode.com/problems/container-with-most-water/description/)
2+
3+
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])`.<br>
4+
Find two lines that together with the x-axis form a container, such that the container contains the most water.<br>
5+
Return the maximum amount of water a container can store.
6+
7+
**Notice** that you may not slant the container.
8+
9+
#### **Example 1:**
10+
11+
<img src="https://raw.githubusercontent.com/leetcoin-releases/leetcode/refs/heads/main/res/ino/477955856.jpg" style="width: 100%; height: 600;"/>
12+
13+
```md
14+
Input: height = [1,8,6,2,5,4,8,3,7]
15+
Output: 49
16+
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.
17+
```
18+
19+
#### **Example 2:**
20+
```md
21+
Input: height = [1,1]
22+
Output: 1
23+
```
24+
25+
#### **Constraints:**
26+
> - `n == height.length`
27+
> - `2 <= n <= 10⁵`
28+
> - `0 <= height[i] <= 10⁴`
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
int maxArea(int* height, int heightSize) {
2+
int left = 0, right = heightSize - 1;
3+
int maxArea = 0;
4+
while (left < right) {
5+
int width = right - left;
6+
int currentHeight = height[left] < height[right] ? height[left] : height[right];
7+
int area = width * currentHeight;
8+
maxArea = maxArea > area ? maxArea : area;
9+
if (height[left] < height[right]) {
10+
left++;
11+
} else {
12+
right--;
13+
}
14+
}
15+
return maxArea;
16+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
int maxArea(vector<int>& height) {
4+
int left = 0;
5+
int right = height.size() - 1;
6+
int max_water = 0;
7+
while (left < right) {
8+
max_water = max(min(height[left], height[right]) * (right - left), max_water);
9+
if (height[left] <= height[right]) {
10+
left++;
11+
} else {
12+
right--;
13+
}
14+
}
15+
return max_water;
16+
}
17+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
public class Solution {
2+
public int MaxArea(int[] height) {
3+
int left = 0, right = height.Length - 1;
4+
int maxArea = 0;
5+
while (left < right) {
6+
int width = right - left;
7+
int currentHeight = Math.Min(height[left], height[right]);
8+
maxArea = Math.Max(maxArea, width * currentHeight);
9+
if (height[left] < height[right]) {
10+
left++;
11+
} else {
12+
right--;
13+
}
14+
}
15+
return maxArea;
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
int maxArea(List<int> height) {
3+
int left = 0, right = height.length - 1;
4+
int maxArea = 0;
5+
while (left < right) {
6+
int width = right - left;
7+
int currentHeight = height[left] < height[right] ? height[left] : height[right];
8+
maxArea = maxArea > (width * currentHeight) ? maxArea : (width * currentHeight);
9+
if (height[left] < height[right]) {
10+
left++;
11+
} else {
12+
right--;
13+
}
14+
}
15+
return maxArea;
16+
}
17+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
-spec max_area(Height :: [integer()]) -> integer().
2+
max_area(Height) ->
3+
Tuple = list_to_tuple(Height),
4+
max_area(Tuple, 0, tuple_size(Tuple) - 1, 0).
5+
max_area(Tuple, Left, Right, MaxWater) when Left < Right ->
6+
Width = Right - Left,
7+
LeftHeight = element(Left + 1, Tuple),
8+
RightHeight = element(Right + 1, Tuple),
9+
Area = min(LeftHeight, RightHeight) * Width,
10+
NewMaxWater = max(MaxWater, Area),
11+
if
12+
LeftHeight < RightHeight ->
13+
max_area(Tuple, Left + 1, Right, NewMaxWater);
14+
true ->
15+
max_area(Tuple, Left, Right - 1, NewMaxWater)
16+
end;
17+
max_area(_Tuple, _Left, _Right, MaxWater) ->
18+
MaxWater.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
defmodule Solution do
2+
@spec max_area(height :: [integer]) :: integer
3+
def max_area(height) do
4+
height = List.to_tuple(height)
5+
do_max_area(height, 0, tuple_size(height) - 1, 0)
6+
end
7+
defp do_max_area(_, l, r, max) when l >= r, do: max
8+
defp do_max_area(height, l, r, max) do
9+
height_l = elem(height, l)
10+
height_r = elem(height, r)
11+
max = max(max, min(height_l, height_r) * (r - l))
12+
if height_l < height_r do
13+
do_max_area(height, l + 1, r, max)
14+
else
15+
do_max_area(height, l, r - 1, max)
16+
end
17+
end
18+
end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
func maxArea(height []int) int {
2+
left, right := 0, len(height)-1
3+
maxArea := 0
4+
for left < right {
5+
width := right - left
6+
currentHeight := height[left]
7+
if height[right] < currentHeight {
8+
currentHeight = height[right]
9+
}
10+
area := width * currentHeight
11+
if area > maxArea {
12+
maxArea = area
13+
}
14+
if height[left] < height[right] {
15+
left++
16+
} else {
17+
right--
18+
}
19+
}
20+
return maxArea
21+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public int maxArea(int[] height) {
3+
int left = 0, right = height.length - 1;
4+
int maxArea = 0;
5+
while (left < right) {
6+
int width = right - left;
7+
int currentHeight = Math.min(height[left], height[right]);
8+
maxArea = Math.max(maxArea, width * currentHeight);
9+
if (height[left] < height[right]) {
10+
left++;
11+
} else {
12+
right--;
13+
}
14+
}
15+
return maxArea;
16+
}
17+
}

0 commit comments

Comments
 (0)