Skip to content

Commit 0177065

Browse files
committed
feat(soobing): week6 > container-with-most-water
1 parent e8c532d commit 0177065

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+
*
3+
* ๋ฌธ์ œ ์„ค๋ช…
4+
* ๊ฐ€์žฅ ๋งŽ์ด ๋‹ด์„ ์ˆ˜ ์žˆ๋Š” ๋ฌผ์˜ ์šฉ๊ธฐ ๊ตฌํ•˜๊ธฐ
5+
* - height: ๋†’์ด(n)๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” ๋ฐฐ์—ด = y์ถ• ๊ฐ’
6+
* - index: x์ถ• ๊ฐ’
7+
*
8+
* ์•„์ด๋””์–ด
9+
* 1. ๋ธŒ๋ฃจํŠธํฌ์Šค ๋ฐฉ์‹ O(n^2)
10+
* - ๋ชจ๋“  ์Œ์„ ๋น„๊ตํ•˜์—ฌ ์ตœ๋Œ€ ๋ฌผ์˜ ์–‘ ์ฐพ๊ธฐ
11+
*
12+
* 2. ํˆฌ ํฌ์ธํ„ฐ ๋ฐฉ์‹ O(n)
13+
* - ์™ผ์ชฝ๊ณผ ์˜ค๋ฅธ์ชฝ ํฌ์ธํ„ฐ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ตœ๋Œ€ ๋ฌผ์˜ ์–‘ ์ฐพ๊ธฐ
14+
* - ๊ฐ™์€ ๋†’์ด์˜ ๋‘ ๋ผ์ธ์ด ์žˆ๋Š” ๊ฒฝ์šฐ ํ•œ์ชฝ๋งŒ ์›€์ง์—ฌ๋„ ์ตœ์ ์˜ ํ•ด๋ฅผ ์ฐพ๋Š”๋ฐ๋Š” ๋ฌธ์ œ ์—†์Œ
15+
*/
16+
function maxArea(height: number[]): number {
17+
let left = 0;
18+
let right = height.length - 1;
19+
let result = 0;
20+
while (left < right) {
21+
const x = right - left;
22+
const y = Math.min(height[left], height[right]);
23+
result = Math.max(x * y, result);
24+
25+
if (height[left] < height[right]) {
26+
left++;
27+
} else {
28+
right--;
29+
}
30+
}
31+
32+
return result;
33+
}

0 commit comments

Comments
ย (0)