-
Notifications
You must be signed in to change notification settings - Fork 0
/
RangeFrequencyQueries.java
71 lines (59 loc) · 2.29 KB
/
RangeFrequencyQueries.java
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
class RangeFreqQuery {
// this is a segment tree
private int startIndexInclusive = 0;
private int endIndexInclusive = 0;
private int mid = 0;
private RangeFreqQuery left = null;
private RangeFreqQuery right = null;
private Map<Integer, Integer> countMap = new HashMap<>();
public RangeFreqQuery(int[] arr) {
this(arr, 0, arr.length - 1);
}
public RangeFreqQuery(int[] arr, int startIndexInclusive, int endIndexInclusive) {
if (arr == null) {
return;
}
this.startIndexInclusive = startIndexInclusive;
this.endIndexInclusive = endIndexInclusive;
this.mid = (startIndexInclusive + endIndexInclusive) / 2;
if (startIndexInclusive == endIndexInclusive) {
// leaf node
countMap.put(arr[startIndexInclusive], 1);
return;
}
if (mid >= startIndexInclusive) {
this.left = new RangeFreqQuery(arr, this.startIndexInclusive, this.mid);
}
if (mid < endIndexInclusive) {
this.right = new RangeFreqQuery(arr, this.mid + 1, this.endIndexInclusive);
}
mergeCountMap(this.left);
mergeCountMap(this.right);
}
public int query(int startIndexInclusive, int endIndexInclusive, int value) {
if (startIndexInclusive > endIndexInclusive) {
return 0;
}
if (startIndexInclusive == this.startIndexInclusive && endIndexInclusive == this.endIndexInclusive) {
return countMap.getOrDefault(value, 0);
}
int count = 0;
if (this.left != null) {
count += this.left.query(startIndexInclusive, Math.min(this.mid, endIndexInclusive), value);
}
if (this.right != null) {
count += this.right.query(Math.max(startIndexInclusive, this.mid + 1), endIndexInclusive, value);
}
return count;
}
public Map<Integer, Integer> getCountMap() {
return countMap;
}
private void mergeCountMap(RangeFreqQuery rfq) {
Map<Integer, Integer> countMap = rfq == null ? Collections.emptyMap() : rfq.getCountMap();
countMap.forEach((k, v) -> {
// can't import MutableInt -_-
this.countMap.put(k, v + this.countMap.getOrDefault(k, 0));
});
}
}