File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed
container-with-most-water Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change
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
+
You canโt perform that action at this time.
0 commit comments