1685. Sum of Absolute Differences in a Sorted Array

Обновлено: 2024-03-12
1 мин
[]

Содержание

LeetCode problem 1685

class Solution:
    def getSumAbsoluteDifferences(self, nums: List[int]) -> List[int]:
        res = []
        s, t = sum(nums), 0
        for i, x in enumerate(nums):
            v = x * i - t + s - t - x * (len(nums) - i)
            res.append(v)
            t += x
        return res