MCPcopy Create free account
hub / github.com/HuberTRoy/leetCode / maxSubArray

Method maxSubArray

Array/MaximumSubarray.py:37–70  ·  view source on GitHub ↗

:type nums: List[int] :rtype: int

(self, nums)

Source from the content-addressed store, hash-verified

35"""
36class Solution(object):
37 def maxSubArray(self, nums):
38 """
39 :type nums: List[int]
40 :rtype: int
41 """
42 if not nums:
43 return 0
44
45 maxes = nums[0]
46 current = nums[0]
47
48 for i in nums[1:]:
49 maxes = max(maxes, current + i, i)
50 if current + i < i:
51 current = i
52 else:
53 current = current + i
54
55 return maxes
56
57 # if not nums:
58 # return 0
59
60 # maxes = ([nums[0]], nums[0])
61 # current = ([nums[0]], nums[0])
62
63 # for i in nums[1:]:
64 # maxes = max(maxes, (current[0]+[i], current[1] + i), ([i], i), key=lambda x: x[1])
65 # if current[1] + i < i:
66 # current = ([i], i)
67 # else:
68 # current = (current[0]+[i], current[1] + i)
69
70 # return maxes[1]

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected