How to use 'maximum sum subarray of size k' in Python

Every line of 'maximum sum subarray of size k' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
13def max_subarray(nums):
14 """
15 :type nums: List[int]
16 :rtype: int
17 """
18 currsum, maxn = 0, float('-inf')
19 for i in range(len(nums)):
20 if currsum + nums[i] > 0:
21 currsum += nums[i]
22 maxn = max(maxn, currsum)
23 else: # to get max of -ve integers
24 currsum = 0
25 maxn = max(maxn, nums[i])
26 return maxn
9def maxSubArray(self, nums):
10 """
11 :type nums: List[int]
12 :rtype: int
13 """
14 if not nums:
15 return 0
16 length = len(nums)
17 current = nums[0]
18 m = current
19 for i in range(1, length):
20 if current < 0:
21 current = 0
22 current += nums[i]
23 m = max(current, m)
24 return m

Related snippets