Skip to content

Commit e02f0d2

Browse files
committed
feat: 문제풀이 추가
1 parent 9f8ed17 commit e02f0d2

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

container-with-most-water/hwanmini.js

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

0 commit comments

Comments
 (0)