Skip to content

Commit 70f36ed

Browse files
committed
add Container With Most Water solution
1 parent 426e28c commit 70f36ed

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* [Problem]: [11] Container With Most Water
3+
* (https://leetcode.com/problems/container-with-most-water/description/)
4+
*/
5+
function maxArea(height: number[]): number {
6+
//시간복잡도 O(n)
7+
//공간복잡도 O(1)
8+
function twoPointerFunc(height: number[]): number {
9+
let left = 0;
10+
let right = height.length - 1;
11+
let result = 0;
12+
13+
while (left < right) {
14+
const length = Math.min(height[left], height[right]);
15+
console.log(length);
16+
const area = (right - left) * length;
17+
18+
result = Math.max(area, result);
19+
20+
if (height[left] < height[right]) {
21+
left++;
22+
} else {
23+
right--;
24+
}
25+
}
26+
27+
return result;
28+
}
29+
30+
return twoPointerFunc(height);
31+
}

0 commit comments

Comments
 (0)