https://leetcode.com/problems/rotate-function/#/description
i是从后向前遍历的
F(1) = F(0) + sum - n * A[i]
public class Solution {
public int maxRotateFunction(int[] A) {
int sum = 0;
int rotate = 0;
for (int i = 0; i < A.length; i++) {
sum += A[i];
rotate += i * A[i];
}
int max = rotate;
for (int i = A.length - 1; i >= 0; i--) {
rotate = rotate + sum - A.length * A[i];
max = Math.max(max, rotate);
}
return max;
}
}
本文介绍了一种求解LeetCode上旋转函数最大值问题的有效算法。该算法通过一次遍历计算数组总和与初始旋转值,然后从后向前迭代更新旋转值并找到最大值。这种方法避免了重复计算,提高了效率。
867

被折叠的 条评论
为什么被折叠?



