class Solution {
public:
/**
* max sum of the subarray
* @param arr int整型vector the array
* @return int整型
*/
int maxsumofSubarray(vector<int>& arr) {
// write code here
int dp[arr.size()]; // 开辟
dp[0] = arr[0];
int total = arr[0];
for(int i = 0;i < arr.size();i++){
dp[i] = max(0,dp[i-1]) + arr[i];
total = max(total,dp[i]);
}
return total;
}
};
牛客网【子数组的最大累加和问题】
最新推荐文章于 2024-08-07 19:04:22 发布