请你设计一个数据结构,它能求出给定子数组内一个给定值的 频率 。
子数组中一个值的 频率 指的是这个子数组中这个值的出现次数。
请你实现 RangeFreqQuery
类:
RangeFreqQuery(int[] arr)
用下标从 0 开始的整数数组arr
构造一个类的实例。int query(int left, int right, int value)
返回子数组arr[left...right]
中value
的 频率 。
一个 子数组 指的是数组中一段连续的元素。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 次。
提示:
1 <= arr.length <= 105
1 <= arr[i], value <= 104
0 <= left <= right < arr.length
- 调用
query
不超过105
次。
class RangeFreqQuery:
def __init__(self, arr: List[int]):
self.mp = defaultdict(list)
for i, x in enumerate(arr):
self.mp[x].append(i)
def query(self, left: int, right: int, value: int) -> int:
if value not in self.mp:
return 0
arr = self.mp[value]
l, r = bisect_right(arr, left - 1), bisect_right(arr, right)
return r - l
# Your RangeFreqQuery object will be instantiated and called as such:
# obj = RangeFreqQuery(arr)
# param_1 = obj.query(left,right,value)
class RangeFreqQuery {
private Map<Integer, List<Integer>> mp = new HashMap<>();
public RangeFreqQuery(int[] arr) {
for (int i = 0; i < arr.length; ++i) {
mp.computeIfAbsent(arr[i], k -> new ArrayList<>()).add(i);
}
}
public int query(int left, int right, int value) {
if (!mp.containsKey(value)) {
return 0;
}
List<Integer> arr = mp.get(value);
int l = search(arr, left - 1);
int r = search(arr, right);
return r - l;
}
private int search(List<Integer> arr, int val) {
int left = 0, right = arr.size();
while (left < right) {
int mid = (left + right) >> 1;
if (arr.get(mid) > val) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
}
}
/**
* Your RangeFreqQuery object will be instantiated and called as such:
* RangeFreqQuery obj = new RangeFreqQuery(arr);
* int param_1 = obj.query(left,right,value);
*/
class RangeFreqQuery {
public:
unordered_map<int, vector<int>> mp;
RangeFreqQuery(vector<int>& arr) {
for (int i = 0; i < arr.size(); ++i)
mp[arr[i]].push_back(i);
}
int query(int left, int right, int value) {
if (!mp.count(value)) return 0;
auto& arr = mp[value];
auto l = upper_bound(arr.begin(), arr.end(), left - 1);
auto r = upper_bound(arr.begin(), arr.end(), right);
return r - l;
}
};
/**
* Your RangeFreqQuery object will be instantiated and called as such:
* RangeFreqQuery* obj = new RangeFreqQuery(arr);
* int param_1 = obj->query(left,right,value);
*/
type RangeFreqQuery struct {
mp map[int][]int
}
func Constructor(arr []int) RangeFreqQuery {
mp := make(map[int][]int)
for i, v := range arr {
mp[v] = append(mp[v], i)
}
return RangeFreqQuery{mp}
}
func (this *RangeFreqQuery) Query(left int, right int, value int) int {
arr := this.mp[value]
l := sort.SearchInts(arr, left)
r := sort.SearchInts(arr, right+1)
return r - l
}
/**
* Your RangeFreqQuery object will be instantiated and called as such:
* obj := Constructor(arr);
* param_1 := obj.Query(left,right,value);
*/