C++ codes with clear formula explanation @75.61%


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

    My derivation:
    Formula Explanation


Log in to reply
 

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