Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add solution and test-cases for problem 2563 #1030

Merged
merged 1 commit into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions leetcode/2501-2600/2563.Count-the-Number-of-Fair-Pairs/README.md
Original file line number Diff line number Diff line change
@@ -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
26 changes: 24 additions & 2 deletions leetcode/2501-2600/2563.Count-the-Number-of-Fair-Pairs/Solution.go
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
}
Loading