Given an array of integers and let N to is its A length.
Assume Bk to is an array obtained by rotating the array A K positions clock-wise, we define a "rotation fu Nction "on as F A follow:
F(k) = 0 * Bk[0] + 1 * Bk[1] + ... + (n-1) * Bk[n-1].
Calculate the maximum value of F(0), F(1), ..., F(n-1) .
Note:
n is guaranteed to being less than 105.
Example:
A = [4, 3, 2, 6]f (0) = (0 * 4) + (1 * 3) + (2 * 2) + (3 * 6) = 0 + 3 + 4 + = 25F (1) = (0 * 6) + (1 * 4) + (2 * 3) + (3 * 2) = 0 + 4 + 6 + 6 = 16F (2) = (0 * 2) + (1 * 6) + (2 * 4) + (3 * 3) = 0 + 6 + 8 + 9 = 23F (3) = (0 * 3) + (1 * 2) + (2 * 6) + (3 * 4) = 0 + 2 + + + = 26So The maximum value of f (0), F (1), F (2), F (3) is f (3) = 26.
Public intMaxrotatefunction (int[] A) {intSize =A.count (); intres =0; for(inti =0; i< size; i++) { intsum =0; intK =i; for(intj =0;j< size;j++) {sum+ = a[j]* ((j+k)%size); } Res= i==0?Sum:Math.Max (res,sum); } returnRes; }
Dp
Public intMaxrotatefunction (int[] A) {intSize =A.count (); if(Size = =0)return 0; varf =New int[size]; intsum =0; intAdd =0; for(inti =0; i< size; i++) {sum+ = a[i]*i; Add+=A[i]; } f[0] =sum; intres =sum; for(inti =1; i< size; i++) {F[i]= f[i-1] + add-size*a[size-i]; Res=Math.max (Res,f[i]); } returnRes; }
396. Rotate Function