Skip to content

Commit c2bf12a

Browse files
committed
add: container-with-most-water
1 parent e428ebe commit c2bf12a

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// 시간복잡도: O(n)
2+
// 공간복잡도: O(1)
3+
4+
/**
5+
* @param {number[]} height
6+
* @return {number}
7+
*/
8+
var maxArea = function (height) {
9+
let maxWater = 0;
10+
let left = 0;
11+
let right = height.length - 1;
12+
13+
while (left < right) {
14+
const lowerHeight = Math.min(height[left], height[right]);
15+
const width = right - left;
16+
maxWater = Math.max(maxWater, lowerHeight * width);
17+
18+
if (height[left] < height[right]) {
19+
left++;
20+
} else {
21+
right--;
22+
}
23+
}
24+
25+
return maxWater;
26+
};

0 commit comments

Comments
 (0)