Skip to content

Commit 25a75aa

Browse files
committed
container with most water solution
1 parent 2576e20 commit 25a75aa

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @param {number[]} height
3+
* @return {number}
4+
*/
5+
var maxArea = function (height) {
6+
let max_area = 0;
7+
let left = 0;
8+
let right = height.length - 1;
9+
while (left < right) {
10+
let current_x = right - left;
11+
let current_y = Math.min(height[left], height[right]);
12+
let current_area = current_x * current_y;
13+
14+
if (current_area > max_area) {
15+
max_area = current_area;
16+
}
17+
if (height[left] < height[right]) {
18+
left = left + 1;
19+
} else {
20+
right = right - 1;
21+
}
22+
}
23+
return max_area;
24+
};
25+
26+
console.log(maxArea([1, 8, 6, 2, 5, 4, 8, 3, 7]));

0 commit comments

Comments
 (0)