c++ solution


  • 0
    B
    class Solution {
    public:
        int maxRotateFunction(vector<int>& A) {
            if(!A.size()) return 0;
            
            int result=0;
            int shift=0, n=A.size(), sum=0;
            
            for(int i:A) sum+=i;
            for(int i=0;i<n;i++) result+=i*A[i];
            
            int cur=result;
            for(int i=n-1;i>0;i--){
                cur=cur+sum-n*A[i];
                result=max(result, cur);
            }
            
            return result;
        }
    };
    

Log in to reply
 

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