-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2080-range-frequency-queries.java
More file actions
31 lines (30 loc) · 1.12 KB
/
2080-range-frequency-queries.java
File metadata and controls
31 lines (30 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class RangeFreqQuery {
int[] arr;
HashMap<Integer, TreeMap<Integer, Integer>> map;
public RangeFreqQuery(int[] arr) {
this.arr = arr;
this.map = new HashMap<>();
fillMap();
}
public void fillMap(){
for (int i = 0; i < arr.length; i++) {
if (!map.containsKey(arr[i])) {
map.put(arr[i], new TreeMap<>());
}
map.get(arr[i]).put(i, map.get(arr[i]).size());
}
}
public int query(int left, int right, int value) {
if (!map.containsKey(value)) return 0;
TreeMap<Integer, Integer> indices = map.get(value);
Integer leftBound = indices.ceilingKey(left);
Integer rightBound = indices.floorKey(right);
if (leftBound == null || rightBound == null) return 0;
return indices.get(rightBound) - indices.get(leftBound) + 1;
}
}
/**
* Your RangeFreqQuery object will be instantiated and called as such:
* RangeFreqQuery obj = new RangeFreqQuery(arr);
* int param_1 = obj.query(left,right,value);
*/