File tree Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * ์ต๋ ๋ถ๋ถ ๋ฐฐ์ด ํฉ(Maximum Subarray) ๋๋ ์นด๋ฐ์ธ ์๊ณ ๋ฆฌ์ฆ(Kadane's Algorithm)
3+ * ์ ์ ๋ฐฐ์ด์ด ์ฃผ์ด์ก์ ๋, ํฉ์ด ์ต๋๊ฐ ๋๋ ์ฐ์๋ ๋ถ๋ถ ๋ฐฐ์ด์ ์ฐพ์ ๊ทธ ํฉ์ ๋ฐํํ๋ ๋ฌธ์
4+ *
5+ * DP๋ฅผ ์ฌ์ฉํ์ฌ ํ์ฌ ์์น๊น์ง์ ๋ถ๋ถํฉ์ ๊ณ์ฐํ๊ณ , ๊ทธ ์ค ์ต๋๊ฐ์ ๊ฐฑ์ ํ๋ ๋ฐฉ์์ผ๋ก ํด๊ฒฐ
6+ */
7+
8+ /**
9+ * @param {number[] } nums
10+ * @return {number }
11+ */
12+ var maxSubArray = function ( nums ) {
13+ if ( nums . length === 0 ) return 0 ; // ๋ฐฐ์ด์ด ๋น์ด์์ผ๋ฉด 0 ๋ฐํ
14+ let maxSum = nums [ 0 ] ; // ์ต๋ ๋ถ๋ถํฉ์ ์ ์ฅ
15+ let currentSum = nums [ 0 ] ; // ํ์ฌ ์์น๊น์ง์ ๋ถ๋ถํฉ
16+
17+ for ( let i = 1 ; i < nums . length ; i ++ ) {
18+ // ํ์ฌ ์์๋ฅผ ํฌํจํ ๋ถ๋ถํฉ๊ณผ ํ์ฌ ์์๋ง ์ ํํ๋ ๊ฒ ์ค ํฐ ๊ฐ์ ์ ํ
19+ currentSum = Math . max ( nums [ i ] , currentSum + nums [ i ] ) ;
20+ maxSum = Math . max ( maxSum , currentSum ) ; // ์ ์ฒด ์ต๋ ๋ถ๋ถํฉ ๊ฐฑ์
21+ }
22+
23+ return maxSum ;
24+ } ;
You canโt perform that action at this time.
0 commit comments