Skip to content

Commit 2e484d9

Browse files
committed
container-with-most-water solution
1 parent e994de4 commit 2e484d9

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+
let result = 0;
7+
let left = 0;
8+
let right = height.length - 1;
9+
10+
while(left < right) {
11+
const leftHeight = height[left];
12+
const rightHeight = height[right];
13+
const width = right - left;
14+
15+
const minHeight = leftHeight < rightHeight ? leftHeight : rightHeight;
16+
const area = minHeight * width;
17+
18+
if (area > result) {
19+
result = area;
20+
}
21+
22+
if (leftHeight <= rightHeight) {
23+
left++;
24+
} else {
25+
right--;
26+
}
27+
}
28+
return result;
29+
};
30+
31+
// 시간 복잡도: O(n)
32+
// 공간 복잡도: O(1)

0 commit comments

Comments
 (0)