-
Notifications
You must be signed in to change notification settings - Fork 819
/
ReversePairsII.java
86 lines (81 loc) · 2.3 KB
/
ReversePairsII.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package divide_and_conquer;
/**
* Created by gouthamvidyapradhan on 01/08/2019 Given an array nums, we call (i, j) an important
* reverse pair if i < j and nums[i] > 2*nums[j].
*
* <p>You need to return the number of important reverse pairs in the given array.
*
* <p>Example1:
*
* <p>Input: [1,3,2,3,1] Output: 2 Example2:
*
* <p>Input: [2,4,3,5,1] Output: 3 Note: The length of the given array will not exceed 50,000. All
* the numbers in the input array are in the range of 32-bit integer.
*
* <p>Solution: O(N log N) Given two sorted arrays A[] and B[] it is quite easy to see for every
* element i in A, how many elements in A have a value > 2 * B[j] using binary search (also possible
* using two pointers) - using this idea we can implement standard merge sort algorithm and for
* every sorted pairs A[] and B[] before we merge we can total number of elements in A which are > 2
* x B[i]
*/
public class ReversePairsII {
public static void main(String[] args) {
int[] A = {2, 4, 3, 5, 1};
System.out.println(new ReversePairsII().reversePairs(A));
}
int answer = 0;
public int reversePairs(int[] nums) {
mergeSort(nums, 0, nums.length - 1);
return answer;
}
private int[] mergeSort(int[] num, int l, int h) {
if (l < h) {
int m = l + (h - l) / 2;
int[] left = mergeSort(num, l, m);
int[] right = mergeSort(num, m + 1, h);
return merge(left, right);
} else if (l == h) {
return new int[] {num[l]};
} else {
return new int[] {};
}
}
private int[] merge(int[] A, int[] B) {
for (int i = 0; i < B.length; i++) {
int num = B[i];
int l = 0, h = A.length;
int index = -1;
while (l < h) {
int m = l + (h - l) / 2;
if ((long) A[m] > (2 * (long) num)) {
index = m;
h = m;
} else {
l = m + 1;
}
}
if (index > -1) {
answer += ((A.length - index));
}
}
int[] C = new int[A.length + B.length];
int k = 0;
int i = 0, j = 0;
for (; i < A.length && j < B.length; ) {
if (A[i] < B[j]) {
C[k++] = A[i];
i++;
} else {
C[k++] = B[j];
j++;
}
}
while (i < A.length) {
C[k++] = A[i++];
}
while (j < B.length) {
C[k++] = B[j++];
}
return C;
}
}