Simple C++ O(n) Solution


  • 0
    F
    class Solution {
    public:
        int maxRotateFunction(vector<int>& nums) {
            int fsum = 0, sum = 0;
            for (int i = 0; i < nums.size(); ++ i)
                fsum += i * nums[i], sum += nums[i];
            int maxval = fsum;
            for (int i = nums.size() - 1; i >= 1; -- i)
                maxval = max(maxval, fsum += sum - nums.size() * nums[i]);
            return maxval;
        }
    };
    

Log in to reply
 

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