Skip to content

Commit 107cd10

Browse files
committed
add: solve DaleStudy#242 Contain With Most Water with ts
1 parent 66d005a commit 107cd10

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

0 commit comments

Comments
ย (0)