Skip to content

Commit f1ff0ef

Browse files
committed
feat: container-with-most-water solution
1 parent 9468c3d commit f1ff0ef

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* 배열의 두개의 높이를 가지고 최대 용량을 구하기
3+
* 알고리즘 복잡도
4+
* - 시간 복잡도: O(n)
5+
* - 공간 복잡도: O(1)
6+
* @param height
7+
*/
8+
function maxArea(height: number[]): number {
9+
// end - start * min(height[start], height[end])가 큰 것
10+
11+
// 8 - 0 * min(1, 7) = 8
12+
// 8 - 1 * min(8, 7) = 49
13+
// 7 - 1 * min(8, 3) = 18
14+
// 6 - 1 * min(8, 8) = 40
15+
// ...
16+
17+
let s = 0
18+
let e = height.length - 1
19+
let curr = 0
20+
let max = 0
21+
22+
while(s < e) {
23+
curr = (e - s) * Math.min(height[s], height[e])
24+
max = Math.max(curr, max)
25+
if(height[s] < height[e]){
26+
s += 1
27+
} else {
28+
e -= 1
29+
}
30+
}
31+
32+
return max
33+
}

0 commit comments

Comments
 (0)