题解 | #牛群的最大能量环#
牛群的最大能量环
https://www.nowcoder.com/practice/653d5a6041a04b8cb9b082eeb1429d1c
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param energy int整型vector
* @return int整型
*/
int maxEnergyCircular(vector<int>& energy) {
// write code here
int n = energy.size();
// 直接在energy的后面插入一个energy数组
// energy.insert(energy.end(), energy.begin(), energy.end());
int ans = INT_MIN;
for(int i=0; i<n; ++i)
{
int sum = 0;
int t = 0;
while(t<n)
{
int index = (i+t)%n;
sum += energy[index];
if(sum<energy[index])
sum = energy[index];
ans = max(ans,sum);
++t;
}
}
return ans;
}
};
正浩创新EcoFlow公司福利 510人发布