Skip to content

Commit f9fedc4

Browse files
authored
Update maximum-subarray.py
1 parent f7db968 commit f9fedc4

1 file changed

Lines changed: 11 additions & 6 deletions

File tree

Python/maximum-subarray.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,20 @@
1212
# If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.
1313
#
1414

15-
class Solution:
16-
# @param A, a list of integers
17-
# @return an integer
18-
def maxSubArray(self, A):
15+
class Solution(object):
16+
def maxSubArray(self, nums):
17+
"""
18+
:type nums: List[int]
19+
:rtype: int
20+
"""
21+
if max(nums) < 0:
22+
return max(nums)
1923
global_max, local_max = float("-inf"), 0
20-
for x in A:
24+
for x in nums:
2125
local_max = max(0, local_max + x)
2226
global_max = max(global_max, local_max)
2327
return global_max
2428

29+
2530
if __name__ == "__main__":
26-
print Solution().maxSubArray([-2,1,-3,4,-1,2,1,-5,4])
31+
print Solution().maxSubArray([-2,1,-3,4,-1,2,1,-5,4])

0 commit comments

Comments
 (0)