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