Here's the solution:
function countSmaller(nums):
def mergeSort(arr):
if len(arr) <= 1:
return arr
mid = len(arr) // 2
left = mergeSort(arr[:mid])
right = mergeSort(arr[mid:])
result = []
i = j = 0
while i < len(left):
while j < len(right) and right[j][0] < left[i][0]:
j += 1
counts[left[i][1]] += j
// merge logic
return merged
counts = [0] * len(nums)
mergeSort(list(enumerate(nums)))
return counts
time, space.