Skip to content

Commit d0368fa

Browse files
committed
feat: 11. Container With Most Water
1 parent 99aa78a commit d0368fa

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// n: len(height)
2+
// Time complexity: O(n)
3+
// Space complexity: O(1)
4+
5+
/**
6+
* @param {number[]} height
7+
* @return {number}
8+
*/
9+
var maxArea = function (height) {
10+
const n = height.length;
11+
let left = 0;
12+
let right = n - 1;
13+
let answer = 0;
14+
15+
while (left < right) {
16+
const w = right - left;
17+
const h = Math.min(height[left], height[right]);
18+
19+
answer = Math.max(w * h, answer);
20+
21+
if (height[left] < height[right]) {
22+
left++;
23+
} else {
24+
right--;
25+
}
26+
}
27+
28+
return answer;
29+
};

0 commit comments

Comments
 (0)