Skip to content

Commit d2b5c4e

Browse files
committed
container with most water solution
1 parent 583ff1e commit d2b5c4e

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @param {number[]} height
3+
* @return {number}
4+
*/
5+
var maxArea = function (height) {
6+
7+
// 투 포인터
8+
// 왼쪽 포인터, 오른쪽 포인터. lower 라인의 포인터를 움직여야함.
9+
10+
let leftPointer = 0;
11+
let rightPointer = height.length - 1;
12+
let tempMax = 0;
13+
14+
while (leftPointer !== rightPointer) {
15+
let areaHeight = Math.min(height[leftPointer], height[rightPointer]);
16+
17+
let areaWidth = rightPointer - leftPointer;
18+
19+
let waterArea = areaHeight * areaWidth;
20+
21+
tempMax = Math.max(tempMax, waterArea);
22+
23+
if (height[leftPointer] < height[rightPointer]) {
24+
leftPointer++;
25+
} else {
26+
rightPointer--;
27+
}
28+
}
29+
30+
return tempMax;
31+
32+
};

0 commit comments

Comments
 (0)