6 ms Java solution O(k*N) time

  • 0
    public class Solution {
        public int maxRotateFunction(int[] A) {
            int length=A.length;
            if(length==0)return 0;
            long temp=F(0,A),_temp=0,result=temp;
            for(int i=0;i<length;i++){
                _temp -= A[i];
            for(int i=0;i<length-1;i++){
                temp += _temp+(long)length*A[i];
            return (int)result;
        private long F(int startIndex,int []A){
            int length=A.length;
            long result=0;
            for(int i=1;i<length;i++){
                result += (long)i*A[(startIndex+i)%length];
            return result;

    this code calculate F just for the first time, this question is similar to my Math Question in highschool.

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.