File tree Expand file tree Collapse file tree 1 file changed +27
-0
lines changed
container-with-most-water Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * https://leetcode.com/problems/container-with-most-water/
3+ * @param {number[] } height
4+ * @return {number }
5+ */
6+ var maxArea = function ( height ) {
7+ let left = 0 ;
8+ let right = height . length - 1 ;
9+ let maxWater = 0 ;
10+
11+ while ( left < right ) {
12+ let width = right - left ; // ํ์ฌ ๋๋น๋ ๋ ํฌ์ธํฐ ์ฌ์ด ๊ฑฐ๋ฆฌ
13+ let minHeight = Math . min ( height [ left ] , height [ right ] ) ; // ํ์ฌ ๋์ด๋ ๋ ์ ์ค ๋ ๋ฎ์ ๊ฐ (๋ฌผ์ด ๋์น์ง ์๊ธฐ ์ํด ๋ฎ์ ์ ๊ธฐ์ค)
14+ let area = width * minHeight ; // ํ์ฌ ๊ตฌ๊ฐ์ด ๋ด์ ์ ์๋ ๋ฌผ์ ์ ๊ณ์ฐ
15+
16+ maxWater = Math . max ( maxWater , area ) ; // ์ต๋ ๋ฌผ ์ ์ฅ๋ ๊ฐฑ์
17+
18+ // ๋ฎ์ ์ชฝ ํฌ์ธํฐ๋ฅผ ์ด๋์์ผ์ ๋ ํฐ ๋์ด๋ฅผ ์ฐพ์
19+ if ( height [ left ] < height [ right ] ) {
20+ left ++ ;
21+ } else {
22+ right -- ;
23+ }
24+ }
25+
26+ return maxWater ;
27+ } ;
You canโt perform that action at this time.
0 commit comments