Skip to content

Commit c96e459

Browse files
committed
container-with-most-water solution
1 parent 29d0b7e commit c96e459

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @param {number[]} height
3+
* @return {number}
4+
*/
5+
6+
var maxArea = function (height) {
7+
let max = 0; // 최대값
8+
let left = 0; // 왼쪽 값
9+
let right = height.length - 1; // 오른쪽 값
10+
11+
// 왼쪽과 오른쪽을 하나씩 줄여가며 가운데서 만날 때 까지 반복
12+
while (left < right) {
13+
// 최대값 = 가로길이(오른쪽 값 - 왼쪽 값) * 세로길이(왼쪽 값, 오른쪽 값 중 더 작은 값)
14+
max = Math.max(max, (right - left) * Math.min(height[left], height[right]));
15+
// 오른쪽 세로가 더 높다면 왼쪽 값 증가
16+
if (height[left] < height[right]) {
17+
left++;
18+
}
19+
else {
20+
right--;
21+
}
22+
}
23+
return max;
24+
};

0 commit comments

Comments
 (0)