Skip to content
LC-2731 Medium LeetCode

2731. Movement of Robots

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 27% Topics: Array, Brainteaser, Sorting, Prefix Sum
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(nlogn)
# Space: O(1)

# sort, math
class Solution(object):
    def sumDistance(self, nums, s, d):
        """
        :type nums: List[int]
        :type s: str
        :type d: int
        :rtype: int
        """
        MOD = 10**9+7
        for i in xrange(len(nums)):
            nums[i] += d if s[i] == 'R' else -d
        nums.sort()
        return reduce(lambda x, y: (x+y)%MOD, ((i-(len(nums)-(i+1)))*x for i, x in enumerate(nums)))

Solution from kamyu104/LeetCode-Solutions · MIT