#2080
Range Frequency Queries
specialist · 980 · lc medium +32 · 42.3% accepted · 745 likes · top 23%
Description
Build a RangeFreqQuery data structure over a 0-indexed integer array arr. Support a query(left, right, value) method that returns how many times value appears in the contiguous subarray arr[left..right] (inclusive). Constructor: RangeFreqQuery(arr). Query: query(left, right, value) -> int.
Example 1:
Input
["RangeFreqQuery", "query", "query"]
[[[12, 33, 4, 56, 22, 2, 34, 33, 22, 12, 34, 56]], [1, 2, 4], [0, 11, 33]]
Output
[null, 1, 2]
Example 2:
Explanation
RangeFreqQuery rangeFreqQuery = new RangeFreqQuery([12, 33, 4, 56, 22, 2, 34, 33, 22, 12, 34, 56]);
rangeFreqQuery.query(1, 2, 4); // return 1. The value 4 occurs 1 time in the subarray [33, 4]
rangeFreqQuery.query(0, 11, 33); // return 2. The value 33 occurs 2 times in the whole array.
Code
1
2
3
4
5
6
7
8
9
10
11
12