forked from Arnon120/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
53. Maximum Subarray.py
41 lines (39 loc) · 1.22 KB
/
53. Maximum Subarray.py
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
33
34
35
36
37
38
39
40
41
class Solution:
# def maxSubArray(self, nums: List[int]) -> int:
def maxSubArray(self, nums) -> int:
"""
input: nums = a list of integers:
len(nums) > 0
"""
nums_improved = list()
so_far = 0
maximal = 0
for num in nums:
maximal = max(maximal,num)
if so_far * num <= 0:
if so_far !=0:
nums_improved.append(so_far)
so_far = num
else:
so_far +=num
if len(nums_improved) == 0:
return maximal
elif len(nums_improved) == 1:
if nums_improved[0] < 0:
return maximal
else: #nums_improved[0] >0 as nums_improved[0] != 0
return nums_improved[0]
if nums_improved[0] <0:
nums_improved.pop(0)
#at this point, we have a list, starting with a positive number, where we know the positives and negatives are alternating.
accume = nums_improved[0]
two_dim_dynamic_programing = [[]]
numsnums = [
[-2,1,-3,4,-1,2,1,-5,4],
[1],
[5,4,-1,7,8],
[1,1,1,1],
[-1,-1,-1,-1],
[-1,1,-1,1,-1,1]
]
res = Solution().maxSubArray(numsnums[0])