File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change 1+ # 최대 부분 배열 합 문제
2+
3+ # O(n^2) time, O(1) space
4+ # Time Limit Exceeded
5+
6+ class Solution :
7+ def maxSubArray (self , nums : List [int ]) -> int :
8+ max_total = nums [0 ]
9+ for i in range (len (nums )):
10+ total = 0
11+ for j in range (i , len (nums )):
12+ total += nums [j ]
13+ max_total = max (total , max_total )
14+ return max_total
15+
16+ # 개선 풀이
17+ # O(n) time, O(1) space
18+ class Solution :
19+ def maxSubArray (self , nums : List [int ]) -> int :
20+ max_total = nums [0 ]
21+ total = nums [0 ]
22+ for i in range (1 , len (nums )):
23+ total = max (nums [i ], total + nums [i ])
24+ max_total = max (total , max_total )
25+ return max_total
26+
27+ # DP 풀이
28+ # O(n) time, O(n) space
29+ class Solution :
30+ def maxSubArray (self , nums : List [int ]) -> int :
31+ dp = [0 ] * len (nums )
32+ dp [0 ] = nums [0 ]
33+ for i in range (1 , len (nums )):
34+ dp [i ] = max (nums [i ], dp [i - 1 ] + nums [i ])
35+ return max (dp )
36+
You can’t perform that action at this time.
0 commit comments