File tree Expand file tree Collapse file tree 1 file changed +49
-0
lines changed
container-with-most-water Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Original file line number Diff line number Diff line change
1
+ /** ์ฒซ๋ฒ์งธ ํ์ด: ์ฌ๊ท
2
+ * ์๊ฐ๋ณต์ก๋: O(2^n)
3
+ */
4
+ /**
5
+ *
6
+ var maxArea = function(height) {
7
+ let maxWater = (height.length - 1) * Math.min(height[0], height[height.length-1]);
8
+ function recursion(start, end){
9
+ if(start >= end){
10
+ // start์ end๊ฐ ๊ฐ๊ฑฐ๋ start๊ฐ end๋ณด๋ค ์ปค์ง ๊ฒฝ์ฐ
11
+ return;
12
+ }
13
+ maxWater = Math.max(maxWater, (end-start)*(Math.min(height[start], height[end])));
14
+ recursion(start+1, end); // ์ผ์ชฝ ๋๋ ค๋ณด๊ณ
15
+ recursion(start, end-1); // ์ฐ์ธก ๋๋ ค๋ณด๊ธฐ
16
+ }
17
+ recursion(0, height.length-1);
18
+ return maxWater;
19
+ };
20
+
21
+ /**
22
+ * ๋๋ฒ์งธ ํ์ด: ํฌํฌ์ธํฐ
23
+ * ์๊ฐ ๋ณต์ก๋: O(n)
24
+ */
25
+ /**
26
+ * @param {number[] } height
27
+ * @return {number }
28
+ */
29
+ var maxArea = function ( height ) {
30
+ let left = 0 ;
31
+ let right = height . length - 1 ;
32
+ let maxWater = 0 ;
33
+
34
+ while ( left < right ) {
35
+ // ํ์ฌ ํฌ์ธํฐ ์์น์์ ๋ฌผ์ ์ ๊ณ์ฐ
36
+ const water = ( right - left ) * Math . min ( height [ left ] , height [ right ] ) ;
37
+ maxWater = Math . max ( maxWater , water ) ;
38
+
39
+ // ๋ ์์ ๋์ด๋ฅผ ๊ฐ์ง ์ชฝ์ ํฌ์ธํฐ๋ฅผ ์ด๋
40
+ // ๋ฌผ์ ์์ ๋ ์์ ๋์ด์ ์ํด ์ ํ๋๊ธฐ ๋๋ฌธ์ ์์ ๋์ด๋ฅผ ๊ฐ๋ฆฌํค๋ ํฌ์ธํฐ๋ฅผ ์ด๋
41
+ if ( height [ left ] < height [ right ] ) {
42
+ left ++ ;
43
+ } else {
44
+ right -- ;
45
+ }
46
+ }
47
+
48
+ return maxWater ;
49
+ } ;
You canโt perform that action at this time.
0 commit comments