连续子数组最大和(*max_element())
连续子数组的最大和
http://www.nowcoder.com/questionTerminal/459bd355da1549fa8a49e350bf3df484
/* 简单dp dp[i]:以array[i]结尾的数组和; 两种状态:要么选择前面dp[i-1],要么不选,取最大值, dp[i] = max(dp[i-1]+ array[i], array[i]) *max_element():求数组中最大值 */ class Solution { public: int FindGreatestSumOfSubArray(vector<int> array) { int ret = array[0]; int tmp = 0; for(auto k : array){ if(tmp + k < 0){ tmp = 0; }else{ tmp += k; } ret = max(ret, tmp); } if(tmp != 0) return ret; return *max_element(array.begin(), array.end()); } };