题目链接
法一
public int maxRotateFunction(int[] nums) {
int len = nums.length, sum = 0;
if(len == 0){
return 0;
}
int[] f = new int[len];
for(int i = 0; i < len; i++){
sum += nums[i];
f[0] += i * nums[i];
}
int max = f[0];
for(int i = 1; i < len; i++){
f[i] = f[i - 1] + sum - len * nums[len - i];
max = Math.max(max, f[i]);
}
return max;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
本地测试
lay.showTitle(396);
Solution396 sol396 = new Solution396();
int[] nums396 = new int[]{4,3,2,6};
System.out.println(sol396.maxRotateFunction(nums396));