Problem

Given an integer array nums, find the subarray with the largest sum, and return its sum.

A subarray is a contiguous non-empty sequence of elements within an array.

https://leetcode.com/problems/maximum-subarray/

Example 1:

Input: nums = [-2,1,-3,4,-1,2,1,-5,4]
Output: 6
Explanation: The subarray [4,-1,2,1] has the largest sum 6.

Example 2:

Input: nums = [1]
Output: 1
Explanation: The subarray [1] has the largest sum 1.

Example 3:

Input: nums = [5,4,-1,7,8]
Output: 23
Explanation: The subarray [5,4,-1,7,8] has the largest sum 23.

Constraints:

  • 1 <= nums.length <= 10^5
  • -10^4 <= nums[i] <= 10^4

Test Cases

1
2
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
solution_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import pytest

from solution import Solution


@pytest.mark.parametrize('nums, expected', [
([-2,1,-3,4,-1,2,1,-5,4], 6),
([1], 1),
([5,4,-1,7,8], 23),
])
class Test:
def test_solution(self, nums, expected):
sol = Solution()
assert sol.maxSubArray(nums) == expected

Thoughts

如果一个 subarray 的和大于 0,把它与下一个数相加的结果一定比下一个数自身大。反之,应该从下一个数开启新的 subarray。

记录当前 subarray 的总和,如果把下一个数加入进来,总和仍然大于 0,就可以继续。否则把 subarray 清空,总和恢复为 0,从再下一个数开始尝试建立新的 subarray。过程中记录见到的最大的总和。

Code

solution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
from typing import List


class Solution:
def maxSubArray(self, nums: List[int]) -> int:
largest = nums[0]
s = 0
for v in nums:
s += v
largest = max(largest, s)
s = max(s, 0)

return largest