Skip to content

Commit b80c227

Browse files
committed
solve: container with most water
1 parent f7961c5 commit b80c227

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

container-with-most-water/wogha95.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* TC: O(H)
3+
* SC: O(1)
4+
* H: height.length
5+
*/
6+
7+
/**
8+
* @param {number[]} height
9+
* @return {number}
10+
*/
11+
var maxArea = function (height) {
12+
let maximumWater = 0;
13+
let left = 0;
14+
let right = height.length - 1;
15+
16+
// 1. 투포인터를 이용하여 양끝에서 모입니다.
17+
while (left < right) {
18+
// 2. 최대 너비값을 갱신해주고
19+
const h = Math.min(height[left], height[right]);
20+
const w = right - left;
21+
22+
maximumWater = Math.max(maximumWater, w * h);
23+
24+
// 3. 왼쪽과 오른쪽 높이 중 더 낮은 쪽의 pointer를 옮깁니다.
25+
if (height[left] < height[right]) {
26+
left += 1;
27+
} else {
28+
right -= 1;
29+
}
30+
}
31+
32+
return maximumWater;
33+
};

0 commit comments

Comments
 (0)