Skip to content

Commit 389f3ce

Browse files
committed
container-with-most-water solution
1 parent 7acd8f7 commit 389f3ce

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

container-with-most-water/jdy8739.js

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 max = 0;
7+
8+
let startIdx = 0;
9+
let endIdx = height.length - 1;
10+
11+
while (startIdx < endIdx) {
12+
const start = height[startIdx];
13+
const end = height[endIdx];
14+
15+
const gap = endIdx - startIdx;
16+
const min = Math.min(start, end);
17+
18+
const area = gap * min;
19+
20+
max = Math.max(max, area);
21+
22+
if (start < end) startIdx++;
23+
else endIdx--;
24+
}
25+
26+
return max;
27+
};
28+
29+
// 시간복잡도 O(n)
30+
// n은 주어진 배열(height)의 길이
31+
32+

0 commit comments

Comments
 (0)