File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed
container-with-most-water Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change 1+ class SolutionMaxArea {
2+ public int maxArea (int [] height ) {
3+ // A์ B ๊ฐ ๋ฉด์ = (A์ B์ X์ถ ์ฐจ์ด) * (A์ B Y์ถ ์ค ๋ ์์ Y์ถ ๊ธธ์ด)
4+ // 1๋ฒ์งธ ํ์ด ๋ฐฉ๋ฒ: ์ ํํ ์ ์๋ ๋ชจ๋ ๊ฒฝ์ฐ์ ๋ํด ์ ๊ฐ์ ๊ตฌํ๋ค, ์๊ฐ๋ณต์ก๋: O(N^2) / ๊ณต๊ฐ๋ณต์ก๋: O(1)
5+ // (์ต์ข
) 2๋ฒ์งธ ํ์ด ๋ฐฉ๋ฒ: ์์ชฝ ๋์์ ํฌํฌ์ธํฐ๋ก ์ขํ์ค๋ฉด์ ๋ฉด์ ์ ๊ตฌํ๋ค, ์๊ฐ๋ณต์ก๋: O(N) / ๊ณต๊ฐ๋ณต์ก๋: O(1)
6+ // ์ ์ฒด ๋๋น๋ฅผ ๊ณ ๋ คํ๋ฉด์ ์์ง์ฌ์ผํ๋ค.
7+ // ๋งค๋ฒ ๋ฉด์ ์ ๊ณ์ฐํ๊ณ , ๋ ํฌ๋ฉด ์ ์ฅํ๋ค.
8+ // lt์ rt ์ค ๋ ๋ฎ์์ชฝ์ ์ขํ๋ค
9+
10+ var lt = 0 ;
11+ var rt = height .length -1 ;
12+ var maxArea = 0 ;
13+
14+ while (lt < rt ) {
15+ var x = rt - lt ;
16+ var y = Math .min (height [lt ], height [rt ]);
17+ var currentArea = x * y ;
18+ if (maxArea < currentArea ) {
19+ maxArea = currentArea ;
20+ }
21+
22+ if (height [lt ] < height [rt ]) {
23+ lt ++;
24+ } else {
25+ rt --;
26+ }
27+ }
28+
29+ return maxArea ;
30+ }
31+ }
You canโt perform that action at this time.
0 commit comments