Skip to content

Commit 72128f9

Browse files
committed
Sync LeetCode submission Runtime - 1485 ms (58.79%), Memory - 35.2 MB (18.21%)
1 parent db7cf33 commit 72128f9

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<p>Given two positive integers <code>left</code> and <code>right</code>, find the two integers <code>num1</code> and <code>num2</code> such that:</p>
2+
3+
<ul>
4+
<li><code>left &lt;= num1 &lt; num2 &lt;= right </code>.</li>
5+
<li>Both <code>num1</code> and <code>num2</code> are <span data-keyword="prime-number">prime numbers</span>.</li>
6+
<li><code>num2 - num1</code> is the <strong>minimum</strong> amongst all other pairs satisfying the above conditions.</li>
7+
</ul>
8+
9+
<p>Return the positive integer array <code>ans = [num1, num2]</code>. If there are multiple pairs satisfying these conditions, return the one with the <strong>smallest</strong> <code>num1</code> value. If no such numbers exist, return <code>[-1, -1]</code><em>.</em></p>
10+
11+
<p>&nbsp;</p>
12+
<p><strong class="example">Example 1:</strong></p>
13+
14+
<pre>
15+
<strong>Input:</strong> left = 10, right = 19
16+
<strong>Output:</strong> [11,13]
17+
<strong>Explanation:</strong> 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+
</pre>
21+
22+
<p><strong class="example">Example 2:</strong></p>
23+
24+
<pre>
25+
<strong>Input:</strong> left = 4, right = 6
26+
<strong>Output:</strong> [-1,-1]
27+
<strong>Explanation:</strong> There exists only one prime number in the given range, so the conditions cannot be satisfied.
28+
</pre>
29+
30+
<p>&nbsp;</p>
31+
<p><strong>Constraints:</strong></p>
32+
33+
<ul>
34+
<li><code>1 &lt;= left &lt;= right &lt;= 10<sup>6</sup></code></li>
35+
</ul>
36+
37+
<p>&nbsp;</p>
38+
<style type="text/css">.spoilerbutton {display:block; border:dashed; padding: 0px 0px; margin:10px 0px; font-size:150%; font-weight: bold; color:#000000; background-color:cyan; outline:0
39+
}
40+
.spoiler {overflow:hidden;}
41+
.spoiler > div {-webkit-transition: all 0s ease;-moz-transition: margin 0s ease;-o-transition: all 0s ease;transition: margin 0s ease;}
42+
.spoilerbutton[value="Show Message"] + .spoiler > div {margin-top:-500%;}
43+
.spoilerbutton[value="Hide Message"] + .spoiler {padding:5px;}
44+
</style>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Approach 1: Sieve of Eratosthenes
2+
3+
# Time: O(R log(log R) + R - L)
4+
# Space: O(R)
5+
6+
class Solution:
7+
def _sieve(self, upper_limit):
8+
sieve = [True] * (upper_limit + 1)
9+
sieve[0] = sieve[1] = False # 0 and 1 are not prime
10+
11+
for number in range(2, int(upper_limit ** 0.5) + 1):
12+
if sieve[number]:
13+
# Mark all multiple of `number` as non-prime
14+
for multiple in range(number * number, upper_limit + 1, number):
15+
sieve[multiple] = False
16+
17+
return sieve
18+
19+
def closestPrimes(self, left: int, right: int) -> List[int]:
20+
sieve_array = self._sieve(right)
21+
22+
prime_numbers = [num for num in range(left, right + 1) if sieve_array[num]]
23+
24+
# Find closest prime pair
25+
if len(prime_numbers) < 2:
26+
return -1, -1
27+
28+
min_diff = float('inf')
29+
closest_pair = (-1, -1)
30+
31+
for index in range(1, len(prime_numbers)):
32+
diff = prime_numbers[index] - prime_numbers[index - 1]
33+
if diff < min_diff:
34+
min_diff = diff
35+
closest_pair = prime_numbers[index - 1], prime_numbers[index]
36+
37+
return closest_pair
38+

0 commit comments

Comments
 (0)