C++ very easy n simple to write O(n)


  • 0
    K

    class Solution {
    public:
    int maxRotateFunction(vector<int>& A) {
    long long p,r,sum=0,t=0,m;
    r=A.size()-1;
    for(int i=0;i<A.size();i++)
    {
    sum+=A[i];
    t+=i*A[i];
    }
    m=t;
    for(int i=1;i<A.size();i++)
    {
    p=A[r-i+1];
    t-=(r)*p;
    t+=sum-p;
    m=max(m,t);
    }
    return m;
    }
    };


Log in to reply
 

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