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