-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmaximum-subarray.py
More file actions
32 lines (26 loc) · 998 Bytes
/
maximum-subarray.py
File metadata and controls
32 lines (26 loc) · 998 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
27
28
29
30
31
32
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
res = nums[0]
for i in range(1, len(nums)):
if nums[i-1] > 0:
nums[i] += nums[i-1]
res = max(res, nums[i])
return res
# using divide and conquer approach
class Solution:
def maxSubArray2(self, nums: List[int]) -> int:
def d_and_d(l, r, nums):
if l == r: return nums[l]
mid = l + (r - l) // 2
left = d_and_d(l, mid, nums)
right = d_and_d(mid+1, r, nums)
l_max = curr = nums[mid]
for i in range(mid-1, l-1, -1):
curr += nums[i]
l_max = max(l_max, curr)
r_max = curr = 0
for i in range(mid+1, r+1, 1):
curr += nums[i]
r_max = max(r_max, curr)
return max(left, right, l_max+r_max)
return d_and_d(0, len(nums)-1, nums)