Analysis:
Use the merge sort to solve the problem, since every segment is sorted, the left part’s rest numbers are surely bigger than the pointered number in the right segment. thus we can have pair number += mid-start+1. Then we can solve it recursively.
Time Complexity:
- O(nlogn)
Space Complexity:
- O(n)
Code
1 | class Solution: |