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
+10
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$.
Copy file name to clipboardexpand all lines: solution/2700-2799/2763.Sum of Imbalance Numbers of All Subarrays/README_EN.md
+14
Original file line number
Diff line number
Diff line change
@@ -55,6 +55,20 @@ The imbalance number of all other subarrays is 0. Hence, the sum of imbalance nu
55
55
56
56
## Solutions
57
57
58
+
**Solution 1: Enumeration + Ordered Set**
59
+
60
+
We can first enumerate the left endpoint $i$ of the subarray. For each $i$, we enumerate the right endpoint $j$ of the subarray from small to large, and maintain all the elements in the current subarray with an ordered list. We also use a variable $cnt$ to maintain the unbalanced number of the current subarray.
61
+
62
+
For each number $nums[j]$, we find the first element $nums[k]$ in the ordered list that is greater than or equal to $nums[j]$, and the last element $nums[h]$ that is less than $nums[j]$:
63
+
64
+
- If $nums[k]$ exists, and the difference between $nums[k]$ and $nums[j]$ is more than $1$, the unbalanced number increases by $1$;
65
+
- If $nums[h]$ exists, and the difference between $nums[j]$ and $nums[h]$ is more than $1$, the unbalanced number increases by $1$;
66
+
- If both $nums[k]$ and $nums[h]$ exist, then inserting the element $nums[j]$ between $nums[h]$ and $nums[k]$ will reduce the unbalanced number by $1$.
67
+
68
+
Then, we add the unbalanced number of the current subarray to the answer, and continue the iteration until we finish iterating over all subarrays.
69
+
70
+
The time complexity is $O(n^2 \times \log n)$ and the space complexity is $O(n)$, where $n$ is the length of the array $nums$.
Copy file name to clipboardexpand all lines: solution/2700-2799/2764.is Array a Preorder of Some Binary Tree/README_EN.md
+14
Original file line number
Diff line number
Diff line change
@@ -48,6 +48,20 @@ For the preorder traversal, first we visit node 0, then we do the preorder trave
48
48
49
49
## Solutions
50
50
51
+
**Solution 1:Depth-First Search**
52
+
53
+
First, we construct a graph $g$ based on the $nodes$ data, where $g[i]$ represents all the child nodes of node $i$.
54
+
55
+
Next, we design a function $dfs(i)$, which represents a pre-order traversal starting from node $i$. We use a variable $k$ to represent the $k$-th node in the $nodes$ list that we have currently traversed, with an initial value of $k = 0$.
56
+
57
+
The execution logic of the function $dfs(i)$ is as follows:
58
+
59
+
If $i \neq nodes[k][0]$, it indicates that the current sequence is not a pre-order traversal sequence of a binary tree, and returns false.
60
+
Otherwise, we increment $k$ by $1$, and then recursively search all child nodes of $i$. If a false is found during the search, we return false immediately. Otherwise, when the search is finished, we return true.
61
+
In the main function, we call $dfs(nodes[0][0])$. If the return value is true and $k = |nodes|$, then the $nodes$ sequence is a pre-order traversal sequence of a binary tree, and we return true; otherwise, we return false.
62
+
63
+
The time complexity is $O(n)$ and the space complexity is $O(n)$, where $n$ is the number of nodes in $nodes$.
0 commit comments