File tree Expand file tree Collapse file tree 1 file changed +19
-6
lines changed Expand file tree Collapse file tree 1 file changed +19
-6
lines changed Original file line number Diff line number Diff line change 3
3
4
4
class Solution :
5
5
def maxSubArray (self , nums : List [int ]) -> int :
6
- global_max_sum = nums [0 ]
7
- local_max_sum = nums [0 ]
8
-
6
+ """
7
+ Find the contiguous subarray with the largest sum using Kadane's algorithm.
8
+
9
+ Args:
10
+ nums: List of integers
11
+
12
+ Returns:
13
+ Maximum subarray sum
14
+ """
15
+ if not nums :
16
+ return 0
17
+
18
+ global_max = local_max = nums [0 ]
19
+
9
20
for num in nums [1 :]:
10
- local_max_sum = max (num , local_max_sum + num )
11
- global_max_sum = max (global_max_sum , local_max_sum )
21
+ # Either start a new subarray with current element or extend previous subarray
22
+ local_max = max (num , local_max + num )
23
+ # Update global maximum if current local maximum is greater
24
+ global_max = max (global_max , local_max )
12
25
13
- return global_max_sum
26
+ return global_max
You can’t perform that action at this time.
0 commit comments