题解 | #牛群的最大能量环#
牛群的最大能量环
https://www.nowcoder.com/practice/653d5a6041a04b8cb9b082eeb1429d1c
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param energy int整型一维数组 * @param energyLen int energy数组长度 * @return int整型 */ int getMaxResult(int* nums, int i, int j) { int sum = 0; int res = -3000; for (int k = i; k <= j; k++) { sum += nums[k]; if (sum > res) res = sum; if (sum < 0) sum = 0; } return res; } int maxEnergyCircular(int* energy, int energyLen ) { int nums[energyLen * 2]; for (int i = 0; i < 2 * energyLen; i++) { if (i < energyLen) nums[i] = energy[i]; else nums[i] = energy[i - energyLen]; } int max = -3000; for (int i = 0; i < energyLen; i++) { int num = getMaxResult(nums, i, i + energyLen - 1); max = fmax(max, num); } return max; }