-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC_2349_DesignaNumberContainerSystem
More file actions
45 lines (40 loc) · 1.13 KB
/
LC_2349_DesignaNumberContainerSystem
File metadata and controls
45 lines (40 loc) · 1.13 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class NumberContainers {
Map<Integer, Integer> map;
Map<Integer, PriorityQueue<Integer>> revMap;
public NumberContainers() {
map = new HashMap<>();
revMap = new HashMap<>();
}
public void change(int index, int number) {
map.put(index, number);
if(!revMap.containsKey(number)){
revMap.put(number, new PriorityQueue<>());
}
revMap.get(number).offer(index);
}
public int find(int number) {
if(!revMap.containsKey(number)){
return -1;
}
PriorityQueue<Integer> pq = revMap.get(number);
while(!pq.isEmpty()){
int idx = pq.peek();
if(map.get(idx) == number){
break;
} else {
pq.poll();
}
}
if(pq.isEmpty()){
revMap.remove(number);
return -1;
}
return revMap.get(number).peek();
}
}
/**
* Your NumberContainers object will be instantiated and called as such:
* NumberContainers obj = new NumberContainers();
* obj.change(index,number);
* int param_2 = obj.find(number);
*/