You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: solution/2700-2799/2761.Prime Pairs With Target Sum/README_EN.md
+10Lines changed: 10 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -43,6 +43,16 @@ These pairs are [3,7] and [5,5], and we return them in the sorted order as descr
43
43
44
44
## Solutions
45
45
46
+
**Solution 1: Preprocessing + Enumeration**
47
+
48
+
First, we pre-process all the prime numbers within the range of $n,ドル and record them in the array $primes,ドル where $primes[i]$ is `true` if $i$ is a prime number.
49
+
50
+
Next, we enumerate $x$ in the range of $[2, \frac{n}{2}]$. In this case, $y = n - x$. If both $primes[x]$ and $primes[y]$ are `true`, then $(x, y)$ is a pair of prime numbers, which is added to the answer.
51
+
52
+
After the enumeration is complete, we return the answer.
53
+
54
+
The time complexity is $O(n \log \log n)$ and the space complexity is $O(n),ドル where $n$ is the number given in the problem.
We can use two pointers, $i$ and $j,ドル to maintain the left and right endpoints of the current subarray, and use an ordered list to maintain all elements in the current subarray.
59
+
60
+
Iterate through the array $nums$. For the current number $nums[i]$ we're iterating over, we add it to the ordered list. If the difference between the maximum and minimum values in the ordered list is greater than 2ドル,ドル we then loop to move the pointer $i$ to the right, continuously removing $nums[i]$ from the ordered list, until the list is empty or the maximum difference between elements in the ordered list is not greater than 2ドル$. At this point, the number of uninterrupted subarrays is $j - i + 1,ドル which we add to the answer.
61
+
62
+
After the iteration, return the answer.
63
+
64
+
The time complexity is $O(n \times \log n)$ and the space complexity is $O(n),ドル where $n$ is the length of the array $nums$.
0 commit comments