@@ -67,7 +67,7 @@ rangeFreqQuery.query(0, 11, 33); // return 2. The value 33 occurs 2 times in the
67
67
68
68
<!-- solution:start -->
69
69
70
- ### Solution 1: Hash Table
70
+ ### Solution 1: Hash Table + Binary Search
71
71
72
72
We use a hash table $g$ to store the array of indices corresponding to each value. In the constructor, we traverse the array $\textit{arr}$, adding the index corresponding to each value to the hash table.
73
73
@@ -214,20 +214,8 @@ class RangeFreqQuery {
214
214
if (! idx ) {
215
215
return 0 ;
216
216
}
217
- const search = (x : number ): number => {
218
- let [l, r] = [0 , idx .length ];
219
- while (l < r ) {
220
- const mid = (l + r ) >> 1 ;
221
- if (idx [mid ] >= x ) {
222
- r = mid ;
223
- } else {
224
- l = mid + 1 ;
225
- }
226
- }
227
- return l ;
228
- };
229
- const l = search (left );
230
- const r = search (right + 1 );
217
+ const l = _ .sortedIndex (idx , left );
218
+ const r = _ .sortedIndex (idx , right + 1 );
231
219
return r - l ;
232
220
}
233
221
}
@@ -239,6 +227,111 @@ class RangeFreqQuery {
239
227
*/
240
228
```
241
229
230
+ #### Rust
231
+
232
+ ``` rust
233
+ use std :: collections :: HashMap ;
234
+
235
+ struct RangeFreqQuery {
236
+ g : HashMap <i32 , Vec <usize >>,
237
+ }
238
+
239
+ impl RangeFreqQuery {
240
+ fn new (arr : Vec <i32 >) -> Self {
241
+ let mut g = HashMap :: new ();
242
+ for (i , & value ) in arr . iter (). enumerate () {
243
+ g . entry (value ). or_insert_with (Vec :: new ). push (i );
244
+ }
245
+ RangeFreqQuery { g }
246
+ }
247
+
248
+ fn query (& self , left : i32 , right : i32 , value : i32 ) -> i32 {
249
+ if let Some (idx ) = self . g. get (& value ) {
250
+ let l = idx . partition_point (| & x | x < left as usize );
251
+ let r = idx . partition_point (| & x | x <= right as usize );
252
+ return (r - l ) as i32 ;
253
+ }
254
+ 0
255
+ }
256
+ }
257
+ ```
258
+
259
+ #### JavaScript
260
+
261
+ ``` js
262
+ /**
263
+ * @param {number[]} arr
264
+ */
265
+ var RangeFreqQuery = function (arr ) {
266
+ this .g = new Map ();
267
+
268
+ for (let i = 0 ; i < arr .length ; ++ i) {
269
+ if (! this .g .has (arr[i])) {
270
+ this .g .set (arr[i], []);
271
+ }
272
+ this .g .get (arr[i]).push (i);
273
+ }
274
+ };
275
+
276
+ /**
277
+ * @param {number} left
278
+ * @param {number} right
279
+ * @param {number} value
280
+ * @return {number}
281
+ */
282
+ RangeFreqQuery .prototype .query = function (left , right , value ) {
283
+ const idx = this .g .get (value);
284
+ if (! idx) {
285
+ return 0 ;
286
+ }
287
+ const l = _ .sortedIndex (idx, left);
288
+ const r = _ .sortedIndex (idx, right + 1 );
289
+ return r - l;
290
+ };
291
+
292
+ /**
293
+ * Your RangeFreqQuery object will be instantiated and called as such:
294
+ * var obj = new RangeFreqQuery(arr)
295
+ * var param_1 = obj.query(left,right,value)
296
+ */
297
+ ```
298
+
299
+ #### C#
300
+
301
+ ``` cs
302
+ public class RangeFreqQuery {
303
+ private Dictionary <int , List <int >> g ;
304
+
305
+ public RangeFreqQuery (int [] arr ) {
306
+ g = new Dictionary <int , List <int >>();
307
+ for (int i = 0 ; i < arr .Length ; ++ i ) {
308
+ if (! g .ContainsKey (arr [i ])) {
309
+ g [arr [i ]] = new List <int >();
310
+ }
311
+ g [arr [i ]].Add (i );
312
+ }
313
+ }
314
+
315
+ public int Query (int left , int right , int value ) {
316
+ if (g .ContainsKey (value )) {
317
+ var idx = g [value ];
318
+ int l = idx .BinarySearch (left );
319
+ int r = idx .BinarySearch (right + 1 );
320
+ l = l < 0 ? - l - 1 : l ;
321
+ r = r < 0 ? - r - 1 : r ;
322
+ return r - l ;
323
+ }
324
+ return 0 ;
325
+ }
326
+ }
327
+
328
+ /**
329
+ * Your RangeFreqQuery object will be instantiated and called as such:
330
+ * RangeFreqQuery obj = new RangeFreqQuery(arr);
331
+ * int param_1 = obj.Query(left, right, value);
332
+ */
333
+ ```
334
+
242
335
<!-- tabs:end -->
243
336
244
337
<!-- solution:end -->
0 commit comments