Skip to content

Commit a8cc8e1

Browse files
committed
feat: solve new question
1 parent df768b3 commit a8cc8e1

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

container-with-most-water/main.js

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

0 commit comments

Comments
 (0)