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/2080.Range Frequency Queries/README_EN.md
+86-41
Original file line number
Diff line number
Diff line change
@@ -47,22 +47,28 @@ rangeFreqQuery.query(0, 11, 33); // return 2. The value 33 occurs 2 times in the
47
47
48
48
## Solutions
49
49
50
-
### Solution 1
50
+
### Solution 1: Hash Table
51
+
52
+
We use a hash table $g$ to store the array of indices corresponding to each value. In the constructor, we traverse the array $\text{arr}$, adding the index corresponding to each value to the hash table.
53
+
54
+
In the query function, we first check whether the given value exists in the hash table. If it does not exist, it means that the value does not exist in the array, so we directly return $0$. Otherwise, we get the index array $\text{idx}$ corresponding to the value. Then we use binary search to find the first index $l$ that is greater than or equal to $\text{left}$, and the first index $r$ that is greater than $\text{right}$. Finally, we return $r - l$.
55
+
56
+
In terms of time complexity, the time complexity of the constructor is $O(n)$, and the time complexity of the query function is $O(\log n)$. The space complexity is $O(n)$. Where $n$ is the length of the array.
0 commit comments