-
-
Notifications
You must be signed in to change notification settings - Fork 422
/
Copy pathReverse_pair.cpp
58 lines (49 loc) · 1.39 KB
/
Reverse_pair.cpp
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
class Solution {
public:
int pairCount = 0;
void merge(vector<int>& nums, int low, int mid, int high) {
int total = 0;
int i = low;
int j = mid + 1;
// Counting the number of pairs satisfying the condition nums[i] > 2 * nums[j]
while (i <= mid) {
while (j <= high && nums[i] > (long long)2 * nums[j]) {
j++;
}
total += (j - (mid + 1));
i++;
}
vector<int> temp;
i = low;
j = mid + 1;
while (i <= mid && j <= high) {
if (nums[i] < nums[j]) {
temp.push_back(nums[i++]);
} else {
temp.push_back(nums[j++]);
}
}
while (i <= mid) {
temp.push_back(nums[i++]);
}
while (j <= high) {
temp.push_back(nums[j++]);
}
for (int it = low; it <= high; it++) {
nums[it] = temp[it - low];
}
pairCount += total;
}
void mergeSort(vector<int>& nums, int low, int high) {
if (low < high) {
int mid = low + (high - low) / 2;
mergeSort(nums, low, mid);
mergeSort(nums, mid + 1, high);
merge(nums, low, mid, high);
}
}
int reversePairs(vector<int>& nums) {
mergeSort(nums, 0, nums.size() - 1);
return pairCount;
}
};