Leetcode Problem 315. Count of Smaller Numbers After Self
315. Count of Smaller Numbers After Self
Leetcode Solutions
Approach: Merge Sort
Define a helper function to perform the merge sort, which also counts the number of smaller elements to the right.
Sort the indices of the array instead of the elements themselves, using a custom comparator based on the values in nums.
During the merge step, for each element from the left sub-array, count the number of elements that have been merged from the right sub-array and update the result for the corresponding index.
After the merge sort is complete, the result array will contain the counts of smaller elements to the right for each index.