Skip to content

Commit a4627b8

Browse files
committed
Apr 19
1 parent 084b338 commit a4627b8

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def countFairPairs(self, nums: List[int], lower: int, upper: int) -> int:
6+
def count_lower(val: int) -> int:
7+
res, j = 0, len(nums) - 1
8+
for i in range(len(nums)):
9+
while i < j and nums[i] + nums[j] > val:
10+
j -= 1
11+
res += max(0, j - i)
12+
return res
13+
nums.sort()
14+
return count_lower(upper) - count_lower(lower - 1)
15+
16+
17+
def main():
18+
nums = [0, 1, 7, 4, 4, 5]
19+
lower = 3
20+
upper = 6
21+
assert Solution().countFairPairs(nums, lower, upper) == 6
22+
23+
nums = [1, 7, 9, 2, 5]
24+
lower = 11
25+
upper = 11
26+
assert Solution().countFairPairs(nums, lower, upper) == 1
27+
28+
29+
if __name__ == '__main__':
30+
main()

2025-04-April-LeetCoding-Challenge/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
| April 16 | [2537. Count the Number of Good Subarrays](https://leetcode.com/problems/count-the-number-of-good-subarrays/) | Medium | Unsolved |
2323
| April 17 | [2176. Count Equal and Divisible Pairs in an Array](https://leetcode.com/problems/count-equal-and-divisible-pairs-in-an-array/) | Easy | Solved |
2424
| April 18 | [38. Count and Say](https://leetcode.com/problems/count-and-say/) | Medium | Solved |
25-
| April 19 | []() | | |
25+
| April 19 | [2563. Count the Number of Fair Pairs](https://leetcode.com/problems/count-the-number-of-fair-pairs/) | Medium | Solved |
2626
| April 20 | []() | | |
2727
| April 21 | []() | | |
2828
| April 22 | []() | | |
@@ -40,5 +40,5 @@
4040
| Level | Problems | Solved | Unsolved |
4141
| --- | --- | --- | --- |
4242
| Easy | 6 | 6 | 0 |
43-
| Medium | 9 | 6 | 3 |
43+
| Medium | 10 | 7 | 3 |
4444
| Hard | 3 | 0 | 3 |

0 commit comments

Comments
 (0)