Skip to content

Commit 41b9640

Browse files
committed
add: containerWithMostWater solution
1 parent 2bc42e6 commit 41b9640

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// tc: O(n);
2+
// sc: O(1);
3+
const maxArea = function (height) {
4+
let max = 0;
5+
let leftIdx = 0;
6+
let rightIdx = height.length - 1;
7+
8+
while (leftIdx < rightIdx) {
9+
const width = rightIdx - leftIdx;
10+
const minHeight = Math.min(height[leftIdx], height[rightIdx]);
11+
const area = width * minHeight;
12+
max = Math.max(max, area);
13+
14+
if (height[leftIdx] > height[rightIdx]) {
15+
rightIdx -= 1;
16+
} else {
17+
leftIdx += 1;
18+
}
19+
}
20+
21+
return max;
22+
};

0 commit comments

Comments
 (0)