Two digits in an array, and if the previous number is greater than the following number, the two digits form a reverse-order pair. Enter an array to find the total number of reverse pairs in this array p. and outputting the result of p to 1000000007 modulo. That is, the output p%1000000007
public class Solution {public static int inversepairs (int [] array) {if (array==null| |
array.length==0) {return 0;
} int[] copy = new Int[array.length];
for (int i=0;i<array.length;i++) {Copy[i] = Array[i];
int count = Inversepairscore (array,copy,0,array.length-1);//The value is too large to find the return count;
private static int Inversepairscore (int[] array,int[] copy,int low,int high) {if (Low==high) {
return 0;
int mid = (Low+high) >>1;
int leftcount = Inversepairscore (array,copy,low,mid)%1000000007;
int rightcount = Inversepairscore (Array,copy,mid+1,high)%1000000007;
int count = 0;
int i=mid;
int J=high;
int loccopy = high; while (I>=low&&j>mid) {if (Array[i]>array[j]) {count = J
-mid; copy[loccopy--]= array[i--];
if (count>=1000000007)//Numerical value is too large to find more than {count%=1000000007;
} else {copy[loccopy--] = array[j--];
} for (; i>=low;i--) {copy[loccopy--]=array[i];
for (; j>mid;j--) {copy[loccopy--]=array[j];
for (int s=low;s<=high;s++) {Array[s] = Copy[s];
Return (Leftcount+rightcount+count)%1000000007; }
}