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
+ *@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
+ }
You canโt perform that action at this time.
0 commit comments