Skip to content

Commit 106a86a

Browse files
committed
Mar-7
1 parent 4eda83a commit 106a86a

File tree

3 files changed

+142
-2
lines changed

3 files changed

+142
-2
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
5555
| [🔲] | [9c-top.md](./list/9c-top.md) | 50/56 | 2 vips |
5656
| [🔲] | [灵茶山艾府.md](./list/灵茶山艾府.md) | 47/2208 | 3 vips |
5757

58-
**Solved**: 575 problems
58+
**Solved**: 576 problems
5959

6060
## 类型/Category
6161

@@ -100,7 +100,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
100100

101101
## Math
102102

103-
| Link | Problem(34) | Solution | Tag | Time | Space | Ref |
103+
| Link | Problem(35) | Solution | Tag | Time | Space | Ref |
104104
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
105105
| [Leetcode-415](https://leetcode.com/problems/add-strings/) | Add Strings | [c++](./leetcode/415.add-strings.cpp), [python3](./leetcode/415.add-strings.py) | Math | O\(N\) | O\(1\) | - |
106106
| [Leetcode-504](https://leetcode.com/problems/base-7/) | Base 7 | [c++](./leetcode/504.base-7.cpp), [python3](./leetcode/504.base-7.py) | Math | O\(N\) | O\(1\) | - |
@@ -109,6 +109,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
109109
| [Leetcode-932](https://leetcode.com/problems/beautiful-array/) | Beautiful Array | [c++](./leetcode/932.beautiful-array.cpp), [python3](./leetcode/932.beautiful-array.py) | Math | O\(N\) | O\(N\) | - |
110110
| [Leetcode-1780](https://leetcode.com/problems/check-if-number-is-a-sum-of-powers-of-three/) | Check If Number Is A Sum Of Powers Of Three | [c++](./leetcode/1780.check-if-number-is-a-sum-of-powers-of-three.cpp), [python3](./leetcode/1780.check-if-number-is-a-sum-of-powers-of-three.py) | Math | O\(LogN\) | O\(1\) | - |
111111
| [Leetcode-3274](https://leetcode.com/problems/check-if-two-chessboard-squares-have-the-same-color/) | Check If Two Chessboard Squares Have The Same Color | [c++](./leetcode/3274.check-if-two-chessboard-squares-have-the-same-color.cpp), [python3](./leetcode/3274.check-if-two-chessboard-squares-have-the-same-color.py) | Math | O\(1\) | O\(1\) | - |
112+
| [Leetcode-2523](https://leetcode.com/problems/closest-prime-numbers-in-range/) | Closest Prime Numbers In Range | [c++](./leetcode/2523.closest-prime-numbers-in-range.cpp), [python3](./leetcode/2523.closest-prime-numbers-in-range.py) | Math | O\(NloglogN\) | O\(N\) | - |
112113
| [Leetcode-3280](https://leetcode.com/problems/convert-date-to-binary/) | Convert Date To Binary | [c++](./leetcode/3280.convert-date-to-binary.cpp), [python3](./leetcode/3280.convert-date-to-binary.py) | Math | O\(1\) | O\(1\) | - |
113114
| [Leetcode-2364](https://leetcode.com/problems/count-number-of-bad-pairs/) | Count Number Of Bad Pairs | [c++](./leetcode/2364.count-number-of-bad-pairs.cpp), [python3](./leetcode/2364.count-number-of-bad-pairs.py) | Math | O\(N\) | O\(N\) | - |
114115
| [Leetcode-204](https://leetcode.com/problems/count-primes/) | Count Primes | [c++](./leetcode/204.count-primes.cpp), [python3](./leetcode/204.count-primes.py) | Math | O\(NlogN\) | O\(N\) | - |
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Tag: Math, Number Theory
2+
// Time: O(NloglogN)
3+
// Space: O(N)
4+
// Ref: -
5+
// Note: -
6+
// Video: https://youtu.be/IQw7Fmb3kUc
7+
8+
// Given two positive integers left and right, find the two integers num1 and num2 such that:
9+
//
10+
// left <= num1 < num2 <= right .
11+
// Both num1 and num2 are prime numbers.
12+
// num2 - num1 is the minimum amongst all other pairs satisfying the above conditions.
13+
//
14+
// 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].
15+
//  
16+
// Example 1:
17+
//
18+
// Input: left = 10, right = 19
19+
// Output: [11,13]
20+
// Explanation: The prime numbers between 10 and 19 are 11, 13, 17, and 19.
21+
// The closest gap between any pair is 2, which can be achieved by [11,13] or [17,19].
22+
// Since 11 is smaller than 17, we return the first pair.
23+
//
24+
// Example 2:
25+
//
26+
// Input: left = 4, right = 6
27+
// Output: [-1,-1]
28+
// Explanation: There exists only one prime number in the given range, so the conditions cannot be satisfied.
29+
//
30+
//  
31+
// Constraints:
32+
//
33+
// 1 <= left <= right <= 106
34+
//
35+
//  
36+
//
37+
//
38+
39+
class Solution {
40+
public:
41+
vector<int> closestPrimes(int left, int right) {
42+
vector<bool> primes = getPrimes(right + 1);
43+
vector<int> res = {-1, -1};
44+
int pre = -1;
45+
int cur = -1;
46+
for (int x = left; x <= right; x++) {
47+
if (primes[x]) {
48+
pre = cur;
49+
cur = x;
50+
}
51+
52+
if (pre > 0 && (res[0] == -1 || cur - pre < res[1] - res[0])) {
53+
res = {pre, cur};
54+
}
55+
}
56+
return res;
57+
}
58+
59+
vector<bool> getPrimes(int n) {
60+
vector<bool> primes(n, true);
61+
primes[0] = primes[1] = false;
62+
63+
for (int i = 2; i < int(sqrt(n)) + 1; i++) {
64+
if (primes[i]) {
65+
for (int j = i * i; j < n; j+=i) {
66+
primes[j] = false;
67+
}
68+
}
69+
}
70+
71+
return primes;
72+
}
73+
};
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Tag: Math, Number Theory
2+
# Time: O(NloglogN)
3+
# Space: O(N)
4+
# Ref: -
5+
# Note: -
6+
# Video: https://youtu.be/IQw7Fmb3kUc
7+
8+
# Given two positive integers left and right, find the two integers num1 and num2 such that:
9+
#
10+
# left <= num1 < num2 <= right .
11+
# Both num1 and num2 are prime numbers.
12+
# num2 - num1 is the minimum amongst all other pairs satisfying the above conditions.
13+
#
14+
# 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].
15+
#  
16+
# Example 1:
17+
#
18+
# Input: left = 10, right = 19
19+
# Output: [11,13]
20+
# Explanation: The prime numbers between 10 and 19 are 11, 13, 17, and 19.
21+
# The closest gap between any pair is 2, which can be achieved by [11,13] or [17,19].
22+
# Since 11 is smaller than 17, we return the first pair.
23+
#
24+
# Example 2:
25+
#
26+
# Input: left = 4, right = 6
27+
# Output: [-1,-1]
28+
# Explanation: There exists only one prime number in the given range, so the conditions cannot be satisfied.
29+
#
30+
#  
31+
# Constraints:
32+
#
33+
# 1 <= left <= right <= 106
34+
#
35+
#  
36+
#
37+
#
38+
39+
class Solution:
40+
def closestPrimes(self, left: int, right: int) -> List[int]:
41+
primes = self.getPrimes(right + 1)
42+
cur = None
43+
pre = None
44+
res = [-1, -1]
45+
46+
for x in range(left, right + 1):
47+
if primes[x]:
48+
pre = cur
49+
cur = x
50+
51+
if pre is not None:
52+
if res[0] == - 1 or cur - pre < res[1] - res[0]:
53+
res = [pre, cur]
54+
55+
return res if res else [-1, -1]
56+
57+
def getPrimes(self, n):
58+
primes = [True for i in range(n)]
59+
primes[0] = primes[1] = False
60+
61+
for i in range(2, int(n ** 0.5) + 1):
62+
if primes[i]:
63+
for j in range(i * i, n, i):
64+
primes[j] = False
65+
66+
return primes

0 commit comments

Comments
 (0)