Two numbers in the array, if a number is greater than the front behind the figures, the two numbers form a reverse pair. Enter an array, this array P. finds the total number of reverse pair P and outputs the result modulo 1000000007. I.e., the output P% 1000000007
Input Description:
Title ensure the same number of the input array is not
data range:
对于%50的数据,size<=10^4
对于%75的数据,size<=10^5
对于%100的数据,size<=2*10^5
Recursive sequencing method
class Solution {
private int[] aux;
private int p;
public int reversePairs(int [] array) {
int len;
if(array == null || (len=array.length) == 0)return 0;
aux = new int[len];
mergeSort(array,0,len - 1);
return p;
}
private void mergeSort(int [] array,int l,int h) {
if(h <= l)return;
int m = l + (h-l) / 2;
mergeSort(array,l,m);
mergeSort(array,m + 1,h);
merge(array,l,m,h);
}
private void merge(int [] array,int l,int m,int h) {
int i = m, j = h;
for(int k = l;k <= h;k++)
aux[k] = array[k];
for(int k = h;k >= l;k--) {
if(i < l) {
array[k] = aux[j--];
}else if(j <= m) {
array[k] = aux[i--];
}else if(aux[i] > aux[j]) {
p += j - m;
array[k] = aux[i--];
}else {
array[k] = aux[j--];
}
}
}
}