File tree Expand file tree Collapse file tree 1 file changed +23
-0
lines changed Expand file tree Collapse file tree 1 file changed +23
-0
lines changed Original file line number Diff line number Diff line change 1+ """
2+ /ํ์ด ๋ด๋ ์ ์ดํด ๋ชปํด์ ์ถ๊ฐ ์ฝ๋ฉํธ/
3+ nums[i]๊ฐ ๊ทธ ์ ๊น์ง subarray์ ํฉ total๋ณด๋ค ์์ ์์์ธ ์ผ์ด์ค๋ ์ด๋ป๊ฒ ๋๋๊ฑฐ์ง ๊ณ ๋ฏผํ๋๋ฐ
4+ ex) total : -1, nums[i] = -2
5+ ์ด์ฐจํผ -1์ธ ์์ ์ maxTotal์ด ์
๋ฐ์ดํธ ๋์ผ๋ฏ๋ก total์ nums[i]๋ถํฐ ๋ํ๊ธฐ ์์ํ๋ค๋ ์๋ฏธ๋ก -2๋ก ์ค์ ํ๋ค๋ ๊ฒ์ ๊นจ๋ฌ์
6+ ๋ฐ๋ผ์ ์ด์ ๊น์ง subarray์ ํฉ๋ง ์์ ์์ ์ฒดํฌ
7+
8+ TC : for๋ฌธ ํ๋ฒ
9+ => O(N)
10+ SC : ์ถ๊ฐ์ ์ธ ๋ฐฐ์ด ๋ฑ ๋ฉ๋ชจ๋ฆฌ ์ฐ์ง ์์ผ๋ฏ๋ก
11+ => O(1)
12+ """
13+ class Solution :
14+ def maxSubArray (self , nums : List [int ]) -> int :
15+ total = nums [0 ]
16+ maxTotal = nums [0 ]
17+ for i in range (1 , len (nums )) :
18+ if (total < 0 ) :
19+ total = nums [i ]
20+ else :
21+ total += nums [i ]
22+ maxTotal = max (total , maxTotal )
23+ return (maxTotal )
You canโt perform that action at this time.
0 commit comments