题目描述
在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。输入一个数组,求出这个数组中的逆序对的总数P。并将P对1000000007取模的结果输出。 即输出P%1000000007
输入描述:
题目保证输入的数组中没有的相同的数字
数据范围:
对于%50的数据,size<=10^4
对于%75的数据,size<=10^5
对于%100的数据,size<=2*10^5
示例1
输入
[1,2,3,4,5,6,7,0]
返回值
7
刚开始的算法,运算结果正确。但是由于递归涉及到大量重复计算的问题,使得运行超时
class Solution { public: int InversePairs(vector<int> data) { if(data.size()==0) return 0; int len = data.size(); int * array = new int[len]; //memset(array, 0, data.size()*sizeof(array)); for(int i=0;i<len;i++){ array[i] = 0; } divideInversePairs(data,0,data.size()-1,array); int sum = 0; for(int i = 0;i<len;i++){ sum += array[i]; } return sum%1000000007; } void CombineInversePairs(vector<int> data,int low,int mid, int high,int array[]){ for(int i = low;i<=mid;i++){//数左右两部分的拟序对数目 for(int j=mid+1;j<=high;j++){ if(data[i]>data[j]) array[i]++; } } }
void divideInversePairs(vector<int> data,int low, int high,int array[]){ if(low<high){ int mid = low + (high - low)/2; divideInversePairs(data, low, mid, array); //分别计算左右两部分的拟序对数,将其存储在array数组中 divideInversePairs(data, mid+1, high, array); CombineInversePairs(data,low,mid,high,array); } } };
查看了剑指offer上的算法思路,意识到自己的算法存在重复计算的问题
使用分治加归并排序的思路,在每次比较完之后将其排序,使得下一次计算不必重复计算,时间复杂度o(nlog(n)),空间复杂度0(n)
class Solution { public: int InversePairs(vector<int> data) { if(data.size()<=0) return 0; int len = data.size(); int * array = new int[len]; //memset(array, 0, data.size()*sizeof(array)); vector<int> copy; for(int i=0;i<len;i++){ // copy.push_back(data[i]); array[i] = data[i]; } int count = divideInversePairs(data,0,len-1,array); free(array); return count%1000000007; } int divideInversePairs(vector<int> &data,int low, int high,int copy[]){ if(low == high){ copy[low] = data[low]; return 0; } int mid = (low+high)>>1; int leftcount = divideInversePairs(data,low,mid,copy)%1000000007; int rightcount = divideInversePairs(data, mid+1,high,copy)%1000000007; int count = 0; int i = mid; int j = high; int index = high; while(i>=low&&j>mid){ if(data[i]>data[j]){//是逆序 count += j - mid; copy[index--] = data[i--]; if(count>=1000000007)//数值过大求余 { count%=1000000007; } }else{ copy[index--] = data[j--]; } } while(i>=low){copy[index--] = data[i--];} while(j>mid){copy[index--] = data[j--];} for(int s=low;s<=high;s++) { data[s] = copy[s]; } return (count + leftcount + rightcount)%1000000007; } };
算法要注意的是:
1.vector<int> &data,这里需要使用引用传递,刚开始以为vector<int>和数组一样传递的是地址
2.数较大,每次计算都要取模,或者使用long long 存储。
在leedcode重新做:
class Solution { private: vector<int> arr; public: int reversePairs(vector<int>& nums) { int n = nums.size(); vector<int> copyVector(n); return reversePairs2(nums,copyVector,0,n-1); } int reversePairs2(vector<int>& nums,vector<int>& copyVector,int left,int right){ if(left>=right) return 0; int mid = left + ((right - left)>>1); int res = reversePairs2(nums,copyVector,left,mid) + reversePairs2(nums,copyVector,mid+1,right); int i = left,j = mid+1,pos = left; while(i<=mid&&j<=right){ if(nums[i]<=nums[j]){ res+=(j- (mid + 1)); copyVector[pos] = nums[i++]; }else{ copyVector[pos] = nums[j++]; } pos++; } while(i<=mid) { copyVector[pos++] = nums[i++]; res += (j - (mid + 1)); } while(j<=right) copyVector[pos++] = nums[j++]; std::copy(copyVector.begin()+left,copyVector.begin()+right+1,nums.begin()+left); return res; } };