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/2000-2099/2007.Find Original Array From Doubled Array/README_EN.md
+40-60
Original file line number
Diff line number
Diff line change
@@ -49,66 +49,59 @@ Other original arrays could be [4,3,1] or [3,1,4].
49
49
50
50
## Solutions
51
51
52
-
### Solution 1: Sorting + Counting + Traversal
52
+
### Solution 1: Sorting
53
53
54
-
First, we check if the length $n$ of the array `changed`is odd. If it is, we directly return an empty array.
54
+
We notice that if the array `changed` is a double array, then the smallest element in the array `changed`must also be an element in the original array. Therefore, we can first sort the array`changed`, and then start from the first element to traverse the array `changed` in ascending order.
55
55
56
-
Then, we sort the array `changed`, and use a hash table or array `cnt` to count the occurrence of each element in `changed`.
56
+
We use a hash table or array $cnt$ to count the occurrence of each element in the array `changed`. For each element $x$ in the array `changed`, we first check whether $x$ exists in $cnt$. If it does not exist, we skip this element. Otherwise, we subtract one from $cnt[x]$, and check whether $x \times 2$ exists in $cnt$. If it does not exist, we return an empty array directly. Otherwise, we subtract one from $cnt[x \times 2]$, and add $x$ to the answer array.
57
57
58
-
Next, we traverse the array `changed`. For each element $x$ in `changed`, we first check if $x$ exists in the hash table `cnt`. If it does not exist, we directly skip this element. Otherwise, we check if $x \times 2$ exists in `cnt`. If it does not exist, we directly return an empty array. Otherwise, we add $x$ to the answer array`ans`, and decrease the occurrence counts of $x$ and $x \times 2$ in `cnt` by $1$ each.
58
+
After the traversal, we return the answer array.
59
59
60
-
After the traversal, we check if the length of the answer array `ans` is $\frac{n}{2}$. If it is, we return `ans`, otherwise we return an empty array.
61
-
62
-
The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array `changed`.
60
+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$, where $n$ is the length of the array `changed`.
0 commit comments