-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path35.java
More file actions
49 lines (46 loc) · 1.36 KB
/
35.java
File metadata and controls
49 lines (46 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
本质是归并排序。
但是需要注意的是中间对于count的处理,要实时对其进行除余,防止越界
*/
public class Solution {
public int InversePairs(int [] array) {
if(array==null || array.length<=0) return 0;
int n=array.length;
int copy[]=new int[n];
for(int i=0;i<n;i++)
copy[i]=array[i];
return helper(array,copy,0,n-1);
}
public int helper(int[] array,int[] copy,int start,int end)
{
if(start==end) return 0;
int count=0;
int mid=start+(end-start)/2;
int leftCount=helper(array,copy,start,mid)%1000000007;
int rightCount=helper(array,copy,mid+1,end)%1000000007;
int i=mid;
int j=end;
int local=end;
while(i>=start && j>mid)
{
if(array[i]>array[j])
{
count+=j-mid;
if(count>=1000000007)
count=count%1000000007;
copy[local--]=array[i--];
}
else
{
copy[local--]=array[j--];
}
}
for(;i>=start;i--)
copy[local--]=array[i];
for(;j>mid;j--)
copy[local--]=array[j];
for(int s=start;s<=end;s++)
array[s]=copy[s];
return (leftCount+rightCount+count)%1000000007;
}
}