classSolution{public:intbestRotation(vector<int>&A){constintn=A.size();// rotate[i] := how many points losing after rotating left i timesvector<int>rotate(n);// Rotating i - A[i] times makes A[i] == its new index// So rotating i - A[i] + 1 times will "start" to make A[i] > its index,// Which is the starting index to lose pointfor(inti=0;i<n;++i)--rotate[(i-A[i]+1+n)%n];// Each time we rotate, we make index 0 to index n - 1,// So we get 1 pointfor(inti=1;i<n;++i)rotate[i]+=rotate[i-1]+1;returndistance(begin(rotate),max_element(begin(rotate),end(rotate)));}};
classSolution{publicintbestRotation(int[]A){finalintn=A.length;// rotate[i] := how many points losing after rotating left i timesint[]rotate=newint[n];// Rotating i - A[i] times makes A[i] == its new index// So rotating i - A[i] + 1 times will "start" to make A[i] > its index,// Which is the starting index to lose pointfor(inti=0;i<n;++i)--rotate[(i-A[i]+1+n)%n];// Each time we rotate, we make index 0 to index n - 1,// So we get 1 pointfor(inti=1;i<n;++i)rotate[i]+=rotate[i-1]+1;intmax=Integer.MIN_VALUE;intmaxIndex=0;for(inti=0;i<n;++i)if(rotate[i]>max){max=rotate[i];maxIndex=i;}returnmaxIndex;}}