1 2 3 4 5 6 7 8 9 10 11 12 13 14
| class Solution { public int maxRotateFunction(int[] nums) { int n = nums.length, ans = 0; int[] sum = new int[n * 2 + 1]; for (int i = 1; i <= 2 * n; ++i) sum[i] = sum[i - 1] + nums[(i - 1) % n]; for (int i = 1; i <= n; ++i) ans += nums[i - 1] * (i - 1); for (int i = n + 1, cur = ans; i < 2 * n; ++i) { cur += nums[(i - 1) % n] * (n - 1); cur -= sum[i - 1] - sum[i - n]; if (cur > ans) ans = cur; } return ans; } }
|