题解 | #牛群的能量#
牛群的能量
https://www.nowcoder.com/practice/00f87ddcd18842d0824d487fd70a730e
#include <algorithm>
#include <climits>
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param energy int整型vector
* @return int整型
*/
int maxEnergy(vector<int>& energy) {
// write code here
int maxSum = energy[0];
int len = energy.size();
vector<int> dp(len);
dp[0] = energy[0];
for (int i = 1; i < len; i++) {
dp[i] = max(dp[i-1] + energy[i], energy[i]);
maxSum = max(maxSum, dp[i]);
}
return maxSum;
}
};

查看2道真题和解析