-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path2563.cpp
More file actions
49 lines (46 loc) · 1.69 KB
/
2563.cpp
File metadata and controls
49 lines (46 loc) · 1.69 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
class Solution {
public:
long long countFairPairs(vector<int>& nums, int lower, int upper) {
sort(nums.begin(), nums.end());
long long res = 0;
int n = nums.size();
for (int i = 1; i < n; ++i) {
long long small = (long long)lower - (long long)nums[i];
long long big = (long long)upper - (long long)nums[i];
// find >= small
auto smallIt = lower_bound(nums.begin(), nums.begin() + i, small);
int smallIdx = smallIt - nums.begin();
// find <= big
auto bigIt = upper_bound(nums.begin(), nums.begin() + i, big);
bigIt--;
int bigIdx = bigIt - nums.begin();
res += (bigIdx - smallIdx) + 1;
}
return res;
}
};
class Solution {
public:
long long countFairPairs(vector<int>& nums, int lower, int upper) {
long long res = 0;
sort(nums.begin(), nums.end());
int n = nums.size();
int rightUpper = n - 1;
int rightLower = n;
for (int left = 0; left < n; ++left) {
while (rightLower > 0 && nums[left] + nums[rightLower - 1] >= lower) {
rightLower--;
}
while (rightUpper > 0 && nums[left] + nums[rightUpper] > upper) {
rightUpper--;
}
int usedRightLower = max(left + 1, rightLower);
if (usedRightLower == n) continue;
if (nums[left] + nums[usedRightLower] < lower) continue;
if (nums[left] + nums[rightUpper] > upper) continue;
if (usedRightLower > rightUpper) continue;
res += (rightUpper - usedRightLower + 1);
}
return res;
}
};