-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReverse_Pairs.java
52 lines (46 loc) · 1.25 KB
/
Reverse_Pairs.java
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
50
51
52
class Solution {
public int reversePairs(int[] nums) {
int ans = mergesort(nums,0,nums.length-1);
return ans ;
}
public static int mergesort(int[] arr, int l , int r ){
if(l>=r){
return 0;
}
int mid = (l+r)/2;
int count = mergesort(arr,l,mid);
count += mergesort(arr,mid+1,r);
count += merge(arr,l,mid,r);
return count;
}
public static int merge(int[] arr, int l , int mid, int r ){
int count = 0;
int j = mid+1;
for(int i=l;i<=mid;i++){
while(j<=r && arr[i] > (2 *(long) arr[j] ) ){
j++;
}
count += (j- (mid+1)) ;
}
ArrayList<Integer> temp = new ArrayList<>();
int i =l, idx = l;
j = mid+1;
while(i<= mid && j<=r ){
if(arr[i]<=arr[j] ){
temp.add(arr[i++] );
}else{
temp.add(arr[j++] );
}
}
while(i<=mid){
temp.add(arr[i++] );
}
while(j<= r){
temp.add(arr[j++] );
}
for( i=l;i<=r;i++){
arr[i] = temp.get(i-l);
}
return count;
}
}