-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1186.py
20 lines (20 loc) · 798 Bytes
/
1186.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution:
def maximumSum(self, arr: List[int]) -> int:
if all(x <= 0 for x in arr):
return max(arr)
neg, l = 0, len(arr)
ans = [[max(0, arr[0])] * l for i in '**']
for i in range(1, l):
ans[0][i] = max(0, ans[0][i - 1] + arr[i])
if arr[i] >= 0:
ans[1][i] = ans[1][i - 1] + arr[i]
else:
if neg == 0:
ans[1][i], neg = ans[1][i - 1], arr[i]
elif ans[0][i - 1] - ans[1][i - 1] >= arr[i]:
ans[1][i], neg = ans[0][i - 1], arr[i]
else:
ans[1][i] = ans[1][i - 1] + arr[i]
if ans[1][i] <= 0:
ans[1][i] = neg = 0
return max(max(ans[0]), max(ans[1]))