O(n) C++ solution


  • 0
    K
    public:
        int maxRotateFunction(vector<int>& A) {
            int len=A.size();
            long long sum=0,allsum=0;
            for(int i=0;i<len;++i)
            {
                sum+=A[i];
                allsum+=i*A[i];
            }
            long long res=allsum;
            for(int i=len-1;i>=1;--i)
            {
                allsum+=sum-len*A[i];
                res=max(res,allsum);
            }
            return res;
        }
    };

Log in to reply
 

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