Skip to content

Commit d581f51

Browse files
committed
Feat: 11. Container With Most Water
1 parent b5a3246 commit d581f51

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* https://leetcode.com/problems/container-with-most-water/
3+
* T.C. O(n)
4+
* S.C. O(1)
5+
*/
6+
function maxArea(height: number[]): number {
7+
let [res, i, j] = [0, 0, height.length - 1];
8+
9+
while (i < j) {
10+
const volume = Math.min(height[i], height[j]) * (j - i);
11+
res = Math.max(res, volume);
12+
13+
if (height[i] < height[j]) {
14+
i++;
15+
} else {
16+
j--;
17+
}
18+
}
19+
20+
return res;
21+
}

0 commit comments

Comments
 (0)