Skip to content

Commit b1420c9

Browse files
authored
Merge pull request #1142 from 0xff-dev/2523
Add solution and test-cases for problem 2523
2 parents f0a9eb1 + 1ba7a3d commit b1420c9

File tree

3 files changed

+89
-13
lines changed

3 files changed

+89
-13
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# [2523.Closest Prime Numbers in Range][title]
2+
3+
## Description
4+
Given two positive integers `left` and `right`, find the two integers `num1` and `num2` such that:
5+
6+
- `left <= num1 < num2 <= right`.
7+
- Both `num1` and `num2` are prime numbers.
8+
- `num2 - num1` is the **minimum** amongst all other pairs satisfying the above conditions.
9+
10+
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]`.
11+
12+
**Example 1:**
13+
14+
```
15+
Input: left = 10, right = 19
16+
Output: [11,13]
17+
Explanation: The prime numbers between 10 and 19 are 11, 13, 17, and 19.
18+
The closest gap between any pair is 2, which can be achieved by [11,13] or [17,19].
19+
Since 11 is smaller than 17, we return the first pair.
20+
```
21+
22+
**Example 2:**
23+
24+
```
25+
Input: left = 4, right = 6
26+
Output: [-1,-1]
27+
Explanation: There exists only one prime number in the given range, so the conditions cannot be satisfied.
28+
```
29+
30+
## 结语
31+
32+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
33+
34+
[title]: https://leetcode.com/problems/closest-prime-numbers-in-range
35+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,46 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "sort"
4+
5+
func calPrime(max int) []int {
6+
isPrime := make([]bool, max+1)
7+
8+
for i := 2; i <= max; i++ {
9+
isPrime[i] = true
10+
}
11+
12+
for i := 2; i*i <= max; i++ {
13+
if isPrime[i] {
14+
for j := i * i; j <= max; j += i {
15+
isPrime[j] = false
16+
}
17+
}
18+
}
19+
20+
var primes []int
21+
for i := 2; i <= max; i++ {
22+
if isPrime[i] {
23+
primes = append(primes, i)
24+
}
25+
}
26+
27+
return primes
28+
}
29+
30+
func Solution(left int, right int) []int {
31+
res := []int{-1, -1}
32+
primes := calPrime(right)
33+
start := sort.Search(len(primes), func(i int) bool {
34+
return primes[i] >= left
35+
})
36+
l := primes[start:]
37+
m := -1
38+
for i := 1; i < len(l); i++ {
39+
if m == -1 || l[i]-l[i-1] < m {
40+
m = l[i] - l[i-1]
41+
res[0] = l[i-1]
42+
res[1] = l[i]
43+
}
44+
}
45+
return res
546
}

leetcode/2501-2600/2523.Closest-Prime-Numbers-in-Range/Solution_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,31 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
left, right int
14+
expect []int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", 10, 19, []int{11, 13}},
17+
{"TestCase2", 4, 6, []int{-1, -1}},
18+
{"TestCase3", 1, 1000000, []int{2, 3}},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.left, c.right)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
27+
c.expect, got, c.left, c.right)
2828
}
2929
})
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)