File tree Expand file tree Collapse file tree 1 file changed +20
-0
lines changed Expand file tree Collapse file tree 1 file changed +20
-0
lines changed Original file line number Diff line number Diff line change
1
+ function maxSubArray ( nums : number [ ] ) : number {
2
+ // ๋ฐฐ์ด์ด ๋น์ด ์๋ ๊ฒฝ์ฐ ์ฒดํฌ (์ ์ฝ์กฐ๊ฑด์ ์ํด ๋ฐ์ํ์ง ์์ง๋ง, ๊ฒฌ๊ณ ํ ์ฝ๋๋ฅผ ์ํด)
3
+ if ( nums . length === 0 ) return 0 ;
4
+
5
+ // ํ์ฌ ๋ถ๋ถ ๋ฐฐ์ด์ ํฉ๊ณผ ์ ์ฒด ์ต๋ ๋ถ๋ถ ๋ฐฐ์ด ํฉ ์ด๊ธฐํ
6
+ let currentSum = nums [ 0 ] ;
7
+ let maxSum = nums [ 0 ] ;
8
+
9
+ // ๋ ๋ฒ์งธ ์์๋ถํฐ ์ํ
10
+ for ( let i = 1 ; i < nums . length ; i ++ ) {
11
+ // ํ์ฌ ์์น์์์ ์ต๋ ๋ถ๋ถ ๋ฐฐ์ด ํฉ ๊ณ์ฐ
12
+ // "์ด์ ๊น์ง์ ํฉ + ํ์ฌ ์์" vs "ํ์ฌ ์์๋ถํฐ ์๋ก ์์"
13
+ currentSum = Math . max ( nums [ i ] , currentSum + nums [ i ] ) ;
14
+
15
+ // ์ ์ฒด ์ต๋๊ฐ ์
๋ฐ์ดํธ
16
+ maxSum = Math . max ( maxSum , currentSum ) ;
17
+ }
18
+
19
+ return maxSum ;
20
+ }
You canโt perform that action at this time.
0 commit comments