C++ O(n) 9 ms


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

Log in to reply
 

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