Design a data structure to find the frequency of a given value in a given subarray.
The frequency of a value in a subarray is the number of occurrences of that value in the subarray.
Implement the RangeFreqQuery class:
RangeFreqQuery(int[] arr) Constructs an instance of the class with the given 0-indexed integer array arr.
int query(int left, int right, int value) Returns the frequency of value in the subarray arr[left...right].
A subarray is a contiguous sequence of elements within an array. arr[left...right] denotes the subarray that contains the elements of nums between indices left and right (inclusive).
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]
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.
classRangeFreqQuery:def__init__(self,arr:List[int]):self.mp=defaultdict(list)fori,xinenumerate(arr):self.mp[x].append(i)defquery(self,left:int,right:int,value:int)->int:ifvaluenotinself.mp:return0arr=self.mp[value]l,r=bisect_right(arr,left-1),bisect_right(arr,right)returnr-l# Your RangeFreqQuery object will be instantiated and called as such:# obj = RangeFreqQuery(arr)# param_1 = obj.query(left,right,value)
classRangeFreqQuery{privateMap<Integer,List<Integer>>mp=newHashMap<>();publicRangeFreqQuery(int[]arr){for(inti=0;i<arr.length;++i){mp.computeIfAbsent(arr[i],k->newArrayList<>()).add(i);}}publicintquery(intleft,intright,intvalue){if(!mp.containsKey(value)){return0;}List<Integer>arr=mp.get(value);intl=search(arr,left-1);intr=search(arr,right);returnr-l;}privateintsearch(List<Integer>arr,intval){intleft=0,right=arr.size();while(left<right){intmid=(left+right)>>1;if(arr.get(mid)>val){right=mid;}else{left=mid+1;}}returnleft;}}/**
* Your RangeFreqQuery object will be instantiated and called as such:
* RangeFreqQuery obj = new RangeFreqQuery(arr);
* int param_1 = obj.query(left,right,value);
*/
classRangeFreqQuery{public:unordered_map<int,vector<int>>mp;RangeFreqQuery(vector<int>&arr){for(inti=0;i<arr.size();++i)mp[arr[i]].push_back(i);}intquery(intleft,intright,intvalue){if(!mp.count(value))return0;auto&arr=mp[value];autol=upper_bound(arr.begin(),arr.end(),left-1);autor=upper_bound(arr.begin(),arr.end(),right);returnr-l;}};/**
* Your RangeFreqQuery object will be instantiated and called as such:
* RangeFreqQuery* obj = new RangeFreqQuery(arr);
* int param_1 = obj->query(left,right,value);
*/
typeRangeFreqQuerystruct{mpmap[int][]int}funcConstructor(arr[]int)RangeFreqQuery{mp:=make(map[int][]int)fori,v:=rangearr{mp[v]=append(mp[v],i)}returnRangeFreqQuery{mp}}func(this*RangeFreqQuery)Query(leftint,rightint,valueint)int{arr:=this.mp[value]l:=sort.SearchInts(arr,left)r:=sort.SearchInts(arr,right+1)returnr-l}/**
* Your RangeFreqQuery object will be instantiated and called as such:
* obj := Constructor(arr);
* param_1 := obj.Query(left,right,value);
*/