Skip to content

Commit e5a39ef

Browse files
committed
solve: container with most water
1 parent 3e99718 commit e5a39ef

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

container-with-most-water/evan.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {number[]} heights
3+
* @return {number}
4+
*/
5+
var maxArea = function (heights) {
6+
let maxAmount = 0;
7+
let [leftEnd, rightEnd] = [0, heights.length - 1];
8+
9+
while (leftEnd < rightEnd) {
10+
const minHeight = Math.min(heights[leftEnd], heights[rightEnd]);
11+
const horizontalLength = rightEnd - leftEnd;
12+
13+
maxAmount = Math.max(maxAmount, minHeight * horizontalLength);
14+
15+
if (heights[leftEnd] > heights[rightEnd]) {
16+
rightEnd -= 1;
17+
} else {
18+
leftEnd += 1;
19+
}
20+
}
21+
22+
return maxAmount;
23+
};

0 commit comments

Comments
 (0)