[Java] Clean Code w/ Explanation
Note that the distance between the robots doesn\'t depend on the direction in which the robots are going to move after the command, because if two robots collid
Note that the distance between the robots doesn\'t depend on the direction in which the robots are going to move after the command, because if two robots collide and change directions, the total sum of distances remains the same.\n\nAlso Note that we don\'t have to actually calculate the movements of each robot. We can calculate the final position of each robot by adding or subtracting d from the initial position, depending on the direction of the robot.\n\nAfter sorting the array of the final positions, we can calculate the total sum of distances using a prefix sum technique.\n\n```\nclass Solution {\n    long MOD = 1000000007l;\n    public int sumDistance(int[] nums, String s, int d) {\n        int n = nums.length;\n        long[] positions = new long[n];\n        long[] prefixSum = new long[n + 1];\n        for (int i = 0; i < n; i++) {\n            positions[i] = nums[i] + (s.charAt(i) == \'R\' ? d : -d);\n        }\n        Arrays.sort(positions);\n        for (int i = 1; i <= n; i++) {\n            prefixSum[i] = prefixSum[i - 1] + positions[i - 1];\n        }\n        long res = 0;\n        for (int i = 0; i < n; i++) {\n            res += positions[i] * i - prefixSum[i];\n            res %= MOD;\n        }\n        return (int) res;\n    }\n}\n```
['Java']