代码
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int ans = 0, n = nums.size();
vector<int> f(n);
f[0] = nums[0];
ans = f[0];
for (int i = 1; i < n; ++i) {
f[i] = max(f[i - 1] + nums[i], nums[i]);
ans = max(ans, f[i]);
}
return ans;
}
};