diff --git a/leetcode/2501-2600/2523.Closest-Prime-Numbers-in-Range/README.md b/leetcode/2501-2600/2523.Closest-Prime-Numbers-in-Range/README.md new file mode 100644 index 000000000..c81d17eea --- /dev/null +++ b/leetcode/2501-2600/2523.Closest-Prime-Numbers-in-Range/README.md @@ -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 diff --git a/leetcode/2501-2600/2523.Closest-Prime-Numbers-in-Range/Solution.go b/leetcode/2501-2600/2523.Closest-Prime-Numbers-in-Range/Solution.go index d115ccf5e..f26cc825f 100755 --- a/leetcode/2501-2600/2523.Closest-Prime-Numbers-in-Range/Solution.go +++ b/leetcode/2501-2600/2523.Closest-Prime-Numbers-in-Range/Solution.go @@ -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 } diff --git a/leetcode/2501-2600/2523.Closest-Prime-Numbers-in-Range/Solution_test.go b/leetcode/2501-2600/2523.Closest-Prime-Numbers-in-Range/Solution_test.go index 14ff50eb4..35e0e16bf 100755 --- a/leetcode/2501-2600/2523.Closest-Prime-Numbers-in-Range/Solution_test.go +++ b/leetcode/2501-2600/2523.Closest-Prime-Numbers-in-Range/Solution_test.go @@ -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() { }