# 面试题42

输入一个整型数组，数组里有正数也有负数。数组中的一个或连续多个整数组成一个子数组。求所有子数组的和的最大值。

要求时间复杂度为O(n)。

```
示例1:

输入: nums = [-2,1,-3,4,-1,2,1,-5,4]
输出: 6
解释: 连续子数组 [4,-1,2,1] 的和最大，为 6。
```

## 提示：

* 1 <= arr.length <= 10^5
* -100 <= arr\[i] <= 100

## 注意：本题与主站 53 题相同：<https://leetcode-cn.com/problems/maximum-subarray/>

## Solutions

1. **dynamic programming**

```cpp
class Solution {
public:
    int maxSubArray(vector<int>& nums) {
        int res = INT_MIN, sum = 0;
        // accumulate only if the final sum is greater than self
        for (auto & n : nums) {
            sum = max(n + sum, n);
            res = max(res, sum);
        }

        return res;
    }
};
```

1. **prefix sum**

```cpp
class Solution {
public:
    int maxSubArray(vector<int>& nums) {
        int mins = 0, sum = 0, res = INT_MIN;
        for (auto n : nums) {
            sum += n;
            res = max(res, sum - mins);
            mins = min(mins, sum);
        }

        return res;
    }
};
```
