题解 | #子数组的最大累加和问题#
子数组的最大累加和问题
http://www.nowcoder.com/practice/554aa508dd5d4fefbf0f86e5fe953abd
动态规划:
动态转移方程:F[i] = max(a[i], F[i-1]+a[i])
class Solution { public: int maxsumofSubarray(vector<int>& arr) { vector<int> F(arr); F[0] = arr[0]>0?arr[0]:0; for(int i=1; i<arr.size(); ++i) F[i] = max(arr[i], F[i-1]+arr[i]); int max_res = INT_MIN; for(int i=0; i<arr.size(); ++i) max_res = max_res<F[i]?F[i]:max_res; return max_res; } };