请你设计一个数据结构,它能求出给定子数组内一个给定值的 频率 。
子数组中一个值的 频率 指的是这个子数组中这个值的出现次数。
请你实现 RangeFreqQuery 类:
一个 子数组 指的是数组中一段连续的元素。arr[left...right] 指的是 nums 中包含下标 left 和 right 在内 的中间一段连续元素。
提示:
示例 1:
输入: ["RangeFreqQuery", "query", "query"] [[[12, 33, 4, 56, 22, 2, 34, 33, 22, 12, 34, 56]], [1, 2, 4], [0, 11, 33]]
输出: [null, 1, 2]
解释: RangeFreqQuery rangeFreqQuery = new RangeFreqQuery([12, 33, 4, 56, 22, 2, 34, 33, 22, 12, 34, 56]); rangeFreqQuery.query(1, 2, 4); // 返回 1 。4 在子数组 [33, 4] 中出现 1 次。 rangeFreqQuery.query(0, 11, 33); // 返回 2 。33 在整个子数组中出现 2 次。
class RangeFreqQuery {
Map> map;
public RangeFreqQuery(int[] arr) {
map = new HashMap<>();
for(int i=0;i list = map.getOrDefault(arr[i],new ArrayList<>());
list.add(i);
map.put(arr[i],list);
}
}
public int query(int left, int right, int value) {
List list = map.get(value);
if(list == null){
return 0;
}else{
int l =LeftBinarySearch(left,list);
int r =RightBinarySearch(right,list);
return r-l;
}
}
public int LeftBinarySearch(int left,List list){
int l = 0;
int r = list.size();
while(l < r){
int mid = (r+l)/2;
if(left <= list.get(mid)) r = mid;
else l = mid + 1;
}
return l;
}
public int RightBinarySearch(int right,List list){
int l = 0;
int r = list.size();
while(l < r){
int mid = (r+l)/2;
if(right >= list.get(mid)) l = mid+1;
else r = mid;
}
return l;
}
}
class RangeFreqQuery{
private int[] a;
public RangeFreqQuery(int[] arr) {
a = arr;
}
public int query(int left, int right, int value) {
int sum=0;
for(int i=left;i<=right;i++){
if(value == a[i]) sum++;
}
return sum;
}
}
public int BinarySearch(int aim,List list,int flag){
int l = 0;
int r = list.size();
if(flag==0){ //找小的,向右边靠
while(l < r){
int mid = (r+l)/2;
if(aim <= list.get(mid)) r = mid;
else l = mid + 1;
}
}
if(flag==1){ //找大的,向左边靠
while(l < r){
int mid = (r+l)/2;
if(aim >= list.get(mid)) l = mid+1;
else r = mid;
}
}
return l;
}
class RangeFreqQuery {
private int[] arr;
private int[] freq;
public RangeFreqQuery(int[] arr) {
int n = arr.length;
this.arr = arr;
this.freq = new int[n];
int[] cnt = new int[10001];
for (int i = 0; i < n; i++) {
freq[i] = cnt[arr[i]]++;
}
}
public int query(int left, int right, int value) {
int l = right, r = left;
for (int i = left; i <= right; i++) {
if (arr[i] == value) {
l = i;
break;
}
}
for (int i = right; i >= l; i--) {
if (arr[i] == value) {
r = i;
break;
}
}
return arr[l] != value ? 0 : freq[r] - freq[l] + 1;
}