Python Solution || Brainteaser? New LeetCode Tag?
Approach\n\n\n\n# Complexity\n- Time complexity:\nO(nlogn)\n\n- Space complexity:\nO(1)\n\n# Code\n\nclass Solution:\n    def sumDistance(self, nums: List[int],
# Approach\n![Untitled board.png](https://assets.leetcode.com/users/images/6f5ce1a3-d22d-46d5-bdbf-50fdb1015052_1708490538.0293803.png)\n\n\n# Complexity\n- Time complexity:\n$$O(nlogn)$$\n\n- Space complexity:\n$$O(1)$$\n\n# Code\n```\nclass Solution:\n    def sumDistance(self, nums: List[int], s: str, d: int) -> int:\n        MOV = lambda c: int(c==\'R\') - int(c==\'L\')\n        MOD = int(1e9+7)\n        ans, prev, n = 0, 0, len(nums)\n\n        for i in range(n):\n            nums[i] += MOV(s[i]) * d\n        nums.sort()\n        \n        for i in range(1, n):\n            prev += n - i\n            ans = (ans + (nums[i] - nums[i-1]) * prev) % MOD\n            prev -= i\n        \n        return ans\n```
['Brainteaser', 'Python3']