Skip to content

Commit d08bcbe

Browse files
committed
add solution : 11. Container With Most Water
1 parent 17451c3 commit d08bcbe

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+
*@link https://leetcode.com/problems/container-with-most-water/description/
3+
*
4+
* ์ ‘๊ทผ ๋ฐฉ๋ฒ• :
5+
* - ๊ฐ€์žฅ ๋งŽ์€ ๋ฌผ์˜ ์–‘์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด์„œ ํˆฌ ํฌ์ธํ„ฐ ์‚ฌ์šฉ
6+
* - ํ•œ ๋†’์ด๊ฐ€ ๋‚ฎ์œผ๋ฉด ๋‹ค๋ฅธ ๋†’์ด๊ฐ€ ์•„๋ฌด๋ฆฌ ๋†’์•„๋„ ์˜๋ฏธ๊ฐ€ ์—†์–ด์„œ, ์ž‘์€ ๋†’์ด๋ฅผ ๊ฐ€์ง„ ํฌ์ธํ„ฐ ์ด๋™ํ•˜๋ฉฐ ๊ณ„์‚ฐ
7+
* - ์–‘ ๋์—์„œ ํฌ์ธํ„ฐ ์ด๋™ํ•  ๋•Œ๋งˆ๋‹ค ์ตœ๋Œ€๊ฐ’ ๊ฐฑ์‹ 
8+
*
9+
* ์‹œ๊ฐ„๋ณต์žก๋„ : O(n)
10+
* - ๋‘ ํฌ์ธํ„ฐ๊ฐ€ ๋ฐฐ์—ด ์–‘ ๋์—์„œ 1๋ฒˆ์”ฉ ์ด๋™ํ•˜๋ฏ€๋กœ
11+
*
12+
* ๊ณต๊ฐ„๋ณต์žก๋„ : O(1)
13+
* - ํฌ์ธํ„ฐ(left,right)์™€ ์ตœ๋Œ€๊ฐ’ ๋ณ€์ˆ˜๋งŒ ์‚ฌ์šฉ
14+
*/
15+
function maxArea(height: number[]): number {
16+
let left = 0,
17+
right = height.length - 1,
18+
maxWater = 0;
19+
20+
while (left < right) {
21+
const width = right - left;
22+
const minHeight = Math.min(height[left], height[right]);
23+
maxWater = Math.max(maxWater, width * minHeight);
24+
25+
if (height[left] < height[right]) {
26+
left++;
27+
} else {
28+
right--;
29+
}
30+
}
31+
32+
return maxWater;
33+
}

0 commit comments

Comments
ย (0)