Skip to content

Add solution and test-cases for problem 2523 #1142

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

Merged
merged 1 commit into from
Mar 7, 2025
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
35 changes: 35 additions & 0 deletions leetcode/2501-2600/2523.Closest-Prime-Numbers-in-Range/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# [2523.Closest Prime Numbers in Range][title]

## Description
Given two positive integers `left` and `right`, find the two integers `num1` and `num2` such that:

- `left <= num1 < num2 <= right`.
- Both `num1` and `num2` are prime numbers.
- `num2 - num1` is the **minimum** amongst all other pairs satisfying the above conditions.

Return the positive integer array `ans = [num1, num2]`. If there are multiple pairs satisfying these conditions, return the one with the **smallest** `num1` value. If no such numbers exist, return `[-1, -1]`.

**Example 1:**

```
Input: left = 10, right = 19
Output: [11,13]
Explanation: The prime numbers between 10 and 19 are 11, 13, 17, and 19.
The closest gap between any pair is 2, which can be achieved by [11,13] or [17,19].
Since 11 is smaller than 17, we return the first pair.
```

**Example 2:**

```
Input: left = 4, right = 6
Output: [-1,-1]
Explanation: There exists only one prime number in the given range, so the conditions cannot be satisfied.
```

## 结语

如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]

[title]: https://leetcode.com/problems/closest-prime-numbers-in-range
[me]: https://github.com/kylesliu/awesome-golang-algorithm
45 changes: 43 additions & 2 deletions leetcode/2501-2600/2523.Closest-Prime-Numbers-in-Range/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,46 @@
package Solution

func Solution(x bool) bool {
return x
import "sort"

func calPrime(max int) []int {
isPrime := make([]bool, max+1)

for i := 2; i <= max; i++ {
isPrime[i] = true
}

for i := 2; i*i <= max; i++ {
if isPrime[i] {
for j := i * i; j <= max; j += i {
isPrime[j] = false
}
}
}

var primes []int
for i := 2; i <= max; i++ {
if isPrime[i] {
primes = append(primes, i)
}
}

return primes
}

func Solution(left int, right int) []int {
res := []int{-1, -1}
primes := calPrime(right)
start := sort.Search(len(primes), func(i int) bool {
return primes[i] >= left
})
l := primes[start:]
m := -1
for i := 1; i < len(l); i++ {
if m == -1 || l[i]-l[i-1] < m {
m = l[i] - l[i-1]
res[0] = l[i-1]
res[1] = l[i]
}
}
return res
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,31 @@ import (
func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
name string
left, right int
expect []int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", 10, 19, []int{11, 13}},
{"TestCase2", 4, 6, []int{-1, -1}},
{"TestCase3", 1, 1000000, []int{2, 3}},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.left, c.right)
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",
c.expect, got, c.left, c.right)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading