From 74dd1af58ddabd9eaa3c87fce990ccf7251514fd Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Wed, 13 Nov 2024 09:27:42 +0800 Subject: [PATCH] Add solution and test-cases for problem 2563 --- .../README.md | 33 +++++++++++++++++++ .../Solution.go | 26 +++++++++++++-- .../Solution_test.go | 20 +++++------ 3 files changed, 67 insertions(+), 12 deletions(-) create mode 100644 leetcode/2501-2600/2563.Count-the-Number-of-Fair-Pairs/README.md diff --git a/leetcode/2501-2600/2563.Count-the-Number-of-Fair-Pairs/README.md b/leetcode/2501-2600/2563.Count-the-Number-of-Fair-Pairs/README.md new file mode 100644 index 000000000..010b772c6 --- /dev/null +++ b/leetcode/2501-2600/2563.Count-the-Number-of-Fair-Pairs/README.md @@ -0,0 +1,33 @@ +# [2563.Count the Number of Fair Pairs][title] + +## Description +Given a **0-indexed** integer array `nums` of size `n` and two integers `lower` and `upper`, return the number of fair pairs. + +A pair `(i, j)` is **fair** if: + +- `0 <= i < j < n`, and +- `lower <= nums[i] + nums[j] <= upper` + + +**Example 1:** + +``` +Input: nums = [0,1,7,4,4,5], lower = 3, upper = 6 +Output: 6 +Explanation: There are 6 fair pairs: (0,3), (0,4), (0,5), (1,3), (1,4), and (1,5). +``` + +**Example 2:** + +``` +Input: nums = [1,7,9,2,5], lower = 11, upper = 11 +Output: 1 +Explanation: There is a single fair pair: (2,3). +``` + +## 结语 + +如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me] + +[title]: https://leetcode.com/problems/count-the-number-of-fair-pairs +[me]: https://github.com/kylesliu/awesome-golang-algorithm diff --git a/leetcode/2501-2600/2563.Count-the-Number-of-Fair-Pairs/Solution.go b/leetcode/2501-2600/2563.Count-the-Number-of-Fair-Pairs/Solution.go index d115ccf5e..a4fb8135f 100755 --- a/leetcode/2501-2600/2563.Count-the-Number-of-Fair-Pairs/Solution.go +++ b/leetcode/2501-2600/2563.Count-the-Number-of-Fair-Pairs/Solution.go @@ -1,5 +1,27 @@ package Solution -func Solution(x bool) bool { - return x +import "sort" + +func Solution(nums []int, lower, upper int) int64 { + sort.Ints(nums) + var ans int64 = 0 + l := len(nums) + for i := 0; i < len(nums); i++ { + // 1, 2, 3, 4 + tmpL := l - i - 1 + leftIndex := sort.Search(tmpL, func(ii int) bool { + return nums[i]+nums[ii+1+i] >= lower + }) + if leftIndex == tmpL { + continue + } + rightIndex := sort.Search(tmpL, func(ii int) bool { + return nums[i]+nums[ii+1+i] > upper + }) + if rightIndex == tmpL && nums[i]+nums[l-1] > upper { + continue + } + ans += int64(rightIndex - leftIndex) + } + return ans } diff --git a/leetcode/2501-2600/2563.Count-the-Number-of-Fair-Pairs/Solution_test.go b/leetcode/2501-2600/2563.Count-the-Number-of-Fair-Pairs/Solution_test.go index 14ff50eb4..46fe51f4f 100755 --- a/leetcode/2501-2600/2563.Count-the-Number-of-Fair-Pairs/Solution_test.go +++ b/leetcode/2501-2600/2563.Count-the-Number-of-Fair-Pairs/Solution_test.go @@ -10,30 +10,30 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + nums []int + l, p int + expect int64 }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{0, 1, 7, 4, 4, 5}, 3, 6, 6}, + {"TestCase2", []int{1, 7, 9, 2, 5}, 11, 11, 1}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.nums, c.l, c.p) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v", + c.expect, got, c.nums, c.l, c.p) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }