How to find reverse pairs in arrays by LeetCode
This article mainly introduces LeetCode how to find the reverse pairs in the array, the article is very detailed, has a certain reference value, interested friends must read it!
Topic description
Two numbers in an array that form a reverse pair if the first number is greater than the next. Enter an array to find the total number of reverse pairs in this array.
0 = e:
Return
M = (s + e) > > 1
# sort and count the left and right parts first
Mergesort (s, m)
Mergesort (m + 1, e)
# use temporary arrays to store sorted left and right parts
# Note that reverse storage is selected here because the smallest element can be obtained by using the pop operation of O (1).
# of course, double-ended queues can also be used here, so that there is no need to reverse order
Left = nums [svm + 1] [::-1]
Right = nums [m + 1 e + 1] [::-1]
For i in range (s, e + 1):
If not right or left and left [- 1]