Posted on

Description

Submission

class Solution {
public:
    vector<int> getSumAbsoluteDifferences(vector<int>& nums) {
        int n = nums.size();
        vector<int> rets(n);

        rets[0] = 0;

        for(int x: nums) {
            rets[0] += x - nums[0]; 
        }

        for(int i = 1; i < n; ++i) {
            rets[i] = rets[i-1] + i * (nums[i] - nums[i-1]) - (n - i) * (nums[i] - nums[i-1]);
        }

        return rets;
    }
};

// {X X X X i-1}{ i} X X X X} 

// rets[i] = rets[i-1] + i * (nums[i] - nums[i-1]) - (n - i) * (nums[i] - nums[i-1]);

Leave a Reply

Your email address will not be published. Required fields are marked *