-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathNumberOfPairsSatisfyingInequality.java
68 lines (64 loc) · 1.81 KB
/
NumberOfPairsSatisfyingInequality.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
/*https://leetcode.com/problems/number-of-pairs-satisfying-inequality/description/*/
// merge sort variant
class Solution {
public long numberOfPairs(int[] nums1, int[] nums2, int diff) {
int[] nums = new int[nums1.length];
for (int i = 0; i < nums.length; ++i)
nums[i] = nums1[i]-nums2[i];
long result = 0;
return mergeSort(nums,0,nums.length-1,diff);
}
static long mergeSort(int[] arr, int low, int high, int diff)
{
long count = 0;
if (low < high)
{
int mid = (low+high)/2;
long left = mergeSort(arr,low,mid,diff);
long right = mergeSort(arr,mid+1,high,diff);
long getCount = merge(arr,low,mid,high,diff);
count += left+right+getCount;
}
return count;
}
static long merge(int[] arr, int low, int mid, int high, int diff)
{
int n1 = mid-low+1;
int n2 = high-mid;
int[] arr1 = new int[n1];
int[] arr2 = new int[n2];
int x = low;
for (int i = 0; i < n1; ++i)
arr1[i] = arr[x++];
for (int j = 0; j < n2; ++j)
arr2[j] = arr[x++];
int i = 0, j = 0;
long count = 0;
x = low;
while (i < n1 && j < n2)
{
if (arr1[i] <= arr2[j]+diff)
{
count += n2-j;
++i;
}
else
++j;
}
i = 0;
j = 0;
x = low;
while (i < n1 && j < n2)
{
if (arr1[i] <= arr2[j])
arr[x++] = arr1[i++];
else
arr[x++] = arr2[j++];
}
while (i < n1)
arr[x++] = arr1[i++];
while (j < n2)
arr[x++] = arr2[j++];
return count;
}
}