-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path53-maximum-subarray
More file actions
54 lines (45 loc) · 1.26 KB
/
53-maximum-subarray
File metadata and controls
54 lines (45 loc) · 1.26 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
class Solution(object):
def maxSubArray(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
maximum = nums[0]
rtn = nums[0]
for i in range(1, len(nums)):
if maximum > 0:
maximum = maximum + nums[i]
else:
maximum = nums[i]
if maximum > rtn:
rtn = maximum
return rtn
class Solution(object):
def maxSubArray(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
dp = [nums[0]]
rtn = dp[0]
for i in range(1, len(nums)):
if dp[i-1] > 0:
dp.append(dp[i-1] + nums[i])
else:
dp.append(nums[i])
rtn = max(rtn, dp[i])
return rtn
class Solution(object):
def maxSubArray(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
dp = [nums[0]]
rtn = dp[0]
for i in range(1, len(nums)):
if dp[i-1] > 0:
dp.append(dp[i-1] + nums[i])
else:
dp.append(nums[i])
return max(dp)