版权声明:本文为博主原创文章,欢迎大家转载,但是要注明我的文章地址。 https://blog.csdn.net/program_developer/article/details/83315829
题目链接:
题目描述:
解题思路:
(1)两层循环遍历,时间复杂度为O()。
顺序扫描整个数组,每扫描到一个数字的时候,逐个比较该数字和它后面的数字的大小。如果后面的数字比它小,则这两个数字就组成一个逆序对。假设数组中含有n个数字,由于每个数字都要和O(n)个数字作比较,因此这个算法的时间复杂度是O()。
代码实现:
public class Solution {
public int InversePairs(int [] array) {
int count = 0;
for(int i=0; i<array.length; i++) {
for(int j=i; j<array.length; j++) {
if(array[i] > array[j])
count++;
}
}
return count;
}
}
结果:不能被AC。
(2)归并排序的思想实现:
已经AC的代码:
public class ArrayInversePairs {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] arr = {7, 5, 6, 4};
System.out.println(InversePairs(arr));
}
public static int InversePairs(int[] array) {
if(array == null || array.length <= 0) {
return 0;
}
int[] tmpArr = new int[array.length];
for(int i = 0; i < array.length; i++) {
tmpArr[i] = array[i];
}
int count = InversePairsCore(array, tmpArr, 0, array.length - 1);
return count;
}
public static int InversePairsCore(int[] arr, int[] tmpArr, int low, int high) {
if(low == high) {
tmpArr[low] = arr[low];
return 0;
}
int len = (high - low) / 2;
int left = InversePairsCore(tmpArr, arr, low, low + len) % 1000000007;
int right = InversePairsCore(tmpArr, arr, low + len + 1, high) % 1000000007;
//p1初始化为前半段最后一个数字的下标
int p1 = low + len;
//p2 初始化为后半段最后一个数字的下标
int p2 = high;
// 开始拷贝的位置
int p3 = high;
// 逆序数
int count = 0;
while(p1 >= low && p2 >= low + len + 1) {
if(arr[p1] > arr[p2]) {
//对应的逆序数
count += p2 - low - len;
tmpArr[p3] = arr[p1];
p3--;
p1--;
if(count >= 1000000007)
count %= 1000000007;
} else {
tmpArr[p3] = arr[p2];
p3--;
p2--;
}
}
while(p1 >= low) {
tmpArr[p3] = arr[p1];
p3--;
p1--;
}
while(p2 >= low + len + 1) {
tmpArr[p3] = arr[p2];
p3--;
p2--;
}
return (count + left + right) % 1000000007;
}
}