Skip to content

Commit 9e1a880

Browse files
committed
container with most water solution
1 parent f1a2829 commit 9e1a880

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

container-with-most-water/limlimjo.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @param {number[]} height
3+
* @return {number}
4+
*/
5+
var maxArea = function (height) {
6+
// Two Pointer를 이용해서 문제를 접근해야할 것 같은데 두 포인터 중에 어떤 거를 이동시켜야하는지 이 부분이 고민이 됨.
7+
// 그래서 이 부분은 풀이 참고
8+
let maxArea = 0;
9+
let start = 0,
10+
end = height.length - 1;
11+
12+
while (start < end) {
13+
// 가로 * 세로
14+
let area = (end - start) * Math.min(height[start], height[end]);
15+
16+
maxArea = Math.max(area, maxArea);
17+
18+
// 고민했던 지점
19+
if (height[start] < height[end]) {
20+
start++;
21+
} else {
22+
end--;
23+
}
24+
}
25+
return maxArea;
26+
};
27+
28+
// 시간복잡도: O(n)
29+
// 공간복잡도: O(1)

0 commit comments

Comments
 (0)