Skip to content

Commit d245c3c

Browse files
committed
container-with-most-water solutions
1 parent d3cf4e7 commit d245c3c

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* @param height - ์ •์ˆ˜ ๋ฐฐ์—ด
3+
* @returns - ์šฉ๊ธฐ์— ๋‹ด์„ ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ ๋ฌผ์˜ ์–‘
4+
* @description
5+
* - ์–‘ ๋์—์„œ 2ํฌ์ธํ„ฐ๋ฅผ ์‚ฌ์šฉํ•ด ํฌ๊ธฐ๋ฅผ ์ฒดํฌํ•ด ๋‚˜์•„๊ฐ
6+
* - ๊ฒฐ๊ตญ ๊ฑฐ๋ฆฌ๊ฐ€ ์งง์•„์ง€๊ธฐ ๋•Œ๋ฌธ์— ์–‘์ชฝ ๋†’์ด๋ฅผ ๋น„๊ตํ•˜๋ฉฐ ๋†’์ด๊ฐ€ ๋‚ฎ์€ ํฌ์ธํ„ฐ๋ฅผ ์›€์ง์ด๋ฉฐ ๊ณ„์‚ฐ
7+
* - ์‹œ๊ฐ„ ๋ณต์žก๋„ O(n)
8+
*/
9+
10+
function maxArea(height: number[]): number {
11+
let left = 0;
12+
let right = height.length - 1;
13+
let water = 0;
14+
15+
while (left < right) {
16+
const calcWater = (right - left) * Math.min(height[left], height[right]);
17+
if (water < calcWater) {
18+
water = calcWater;
19+
}
20+
21+
if (height[left] < height[right]) {
22+
left++;
23+
} else {
24+
right--;
25+
}
26+
}
27+
28+
return water;
29+
}
30+
31+
const height = [1, 3, 2, 5, 25, 24, 5];
32+
const result = maxArea(height);
33+
34+

0 commit comments

Comments
ย (0)