-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathRangeFrequencyQueries.java
67 lines (64 loc) · 2.02 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
/*https://leetcode.com/problems/range-frequency-queries/*/
class RangeFreqQuery {
Map<Integer,TreeSet<Integer>> rough;
Map<Integer,int[]> map;
public RangeFreqQuery(int[] arr) {
rough = new HashMap<Integer,TreeSet<Integer>>();
map = new HashMap<Integer,int[]>();
for (int i = 0; i < arr.length; ++i)
{
rough.putIfAbsent(arr[i],new TreeSet<Integer>());
rough.get(arr[i]).add(i);
}
for (Map.Entry entry : rough.entrySet())
{
TreeSet<Integer> set = (TreeSet)entry.getValue();
int[] value = new int[set.size()];
int index = 0;
for (Integer val : set)
value[index++] = val;
map.put((Integer)entry.getKey(),value);
}
// for (Map.Entry entry : map.entrySet())
// {
// System.out.print(entry.getKey()+"->");
// for (int obj : (int[])entry.getValue())
// System.out.print(obj+",");
// System.out.println();
// }
}
public int query(int left, int right, int value) {
int[] indices = map.get(value);
if (indices == null) return 0;
int low = 0, high = indices.length-1, mid, start = -1, end = -1;
while (low <= high)
{
mid = low+(high-low)/2;
if (indices[mid] >= left)
{
start = mid;
high = mid-1;
}
else low = mid+1;
}
low = 0; high = indices.length-1;
while (low <= high)
{
mid = low+(high-low)/2;
if (indices[mid] <= right)
{
end = mid;
low = mid+1;
}
else high = mid-1;
}
if (start != -1 && end != -1)
return end-start+1;
else return 0;
}
}
/**
* Your RangeFreqQuery object will be instantiated and called as such:
* RangeFreqQuery obj = new RangeFreqQuery(arr);
* int param_1 = obj.query(left,right,value);
*/