题解 | #牛群的最大能量环#
牛群的最大能量环
https://www.nowcoder.com/practice/653d5a6041a04b8cb9b082eeb1429d1c
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param energy int整型vector * @return int整型 */ int maxEnergyCircular(vector<int>& energy) { // write code here energy.insert(energy.end(),energy.begin(),energy.end()); int* dp = new int[energy.size()]; memset(dp,0,sizeof(int) * energy.size()); dp[0] = energy[0]; int result = energy[0]; for (int i = 1;i < energy.size();++i) { if (energy[i] < 0) { dp[i] = 0; continue; } else { dp[i] = dp[i - 1] + energy[i]; } result = max(dp[i],result); } for (int i = 0;i < energy.size();++i) { cout << dp[i] << " "; } return result; } };