Commit 5871d7e
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
File tree
20 files changed
+341
-0
lines changed- res/ino
- sol/solution/0001-0100/0011
20 files changed
+341
-0
lines changedLoading
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
0 commit comments