🦅 Python3 🎃 Clear Code with Intuition and Brainstorm Procedure
Intuition\nI can\'t solve this problem until realize the first difficulty, changing direction, it is a honey pot to decrease our focus.\nAnd second difficulty i
# Intuition\nI can\'t solve this problem until realize the first difficulty, changing direction, it is a honey pot to decrease our focus.\nAnd second difficulty is how to get distances sum in sufficient time complexity.\n\n# Approach\nHow to Solve Second Difficulty:\n> When nums is unordered list, we need to use abs() to get positive distances.\n        If ordered nums to increasing order, we can calculate distance by simply substract each items.\n\n> eg:\n>          unordered nums = [3, -1, 2], then distances need to use abs() like this:\n>          result = abs(-1 - 3) + abs(2 - 3) + abs(2 - (-1)) \n> ordered nums = [-1, 2, 3], then distances like this:\n          result = (2 - (-1)) + (3 - (-1)) + (3 - 2) = (2 * 1 - (-1)) + (3 * 2 - (-1 + 2))\n\n# Complexity\n\n- Time complexity:\n$$O(NLogN)$$\n\n- Space complexity:\n$$O(N)$$\n\n# Code\n## 1. Change Moving Direction, ERROR: "Time Limit Exceeded"\n```python []\nclass Solution:\n    def sumDistance(self, nums: List[int], s: str, d: int) -> int:\n        MOD = 10**9+7\n        for _ in range(d):\n            mapping = defaultdict(list)\n            for index in range(len(nums)):\n                target = nums[index] + 1 if s[index] == \'R\' else nums[index] - 1\n                mapping[target].append(index)\n                nums[index] = target\n            for k, item in mapping.items():\n                if len(item) > 1:\n                    for i in item:\n                        s = list(s)\n                        s[i] = \'R\' if s[i] == \'L\' else \'L\'\n                        s = \'\'.join(s)\n        result = 0\n        for i in range(len(nums)):\n            for j in range(i+1, len(nums)):\n                result = (result + (abs(nums[i] - nums[j]) % MOD)) % MOD\n        return result\n```\n\n## 2. Force Sum, ERROR: "Time Limit Exceeded"\n```python []\nclass Solution:\n    def sumDistance(self, nums: List[int], s: str, d: int) -> int:\n        MOD = 10**9+7\n        for index in range(len(nums)):\n            nums[index] += d if s[index] == \'R\' else -d\n        result = 0\n        for i in range(len(nums)):\n            for j in range(i+1, len(nums)):\n                result = (result + (abs(nums[i] - nums[j]) % MOD)) % MOD\n        return result\n```\n\n## 3. Order Positions from smallest to largest\n```python []\nclass Solution:\n    def sumDistance(self, nums: List[int], s: str, d: int) -> int:\n        for index in range(len(nums)):\n            nums[index] += d if s[index] == \'R\' else -d\n\n        MOD = 10**9+7\n        nums.sort()\n        result = 0\n        substract = nums[0]\n        for index in range(1, len(nums)):\n            result = (result + nums[index] * index % MOD - substract) % MOD\n            substract = (substract + nums[index]) % MOD\n        return result\n```
['Python3']