forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsounmind.py
More file actions
26 lines (19 loc) · 708 Bytes
/
sounmind.py
File metadata and controls
26 lines (19 loc) · 708 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
from typing import List
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
"""
Find the contiguous subarray with the largest sum using Kadane's algorithm.
Args:
nums: List of integers
Returns:
Maximum subarray sum
"""
if not nums:
return 0
global_max = local_max = nums[0]
for num in nums[1:]:
# Either start a new subarray with current element or extend previous subarray
local_max = max(num, local_max + num)
# Update global maximum if current local maximum is greater
global_max = max(global_max, local_max)
return global_max