-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPriceIndex.java
More file actions
47 lines (40 loc) · 910 Bytes
/
Copy pathPriceIndex.java
File metadata and controls
47 lines (40 loc) · 910 Bytes
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
import java.util.TreeMap;
/**
* Collection of item counts indexed by price
*
* @author G94
*/
public class PriceIndex {
TreeMap<Long, Count> map; /* item counts indexed by price */
public PriceIndex() {
this.map = new TreeMap<>();
}
void increment(long price) {
Count count = map.get(price);
if (count == null)
map.put(price, new Count(1));
else
count.value++;
}
void decrement(long price) {
Count count = map.get(price);
if (count != null) {
if (count.value == 1)
map.remove(price);
else
count.value--;
}
}
long findMinPrice() {
return map.size() > 0 ? map.firstKey() : 0;
}
long findMaxPrice() {
return map.size() > 0 ? map.lastKey() : 0;
}
int range(long lowPrice, long highPrice) {
int count = 0;
for (Count c : map.subMap(lowPrice, true, highPrice, true).values())
count += c.value;
return count;
}
}