-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDesignANumberContainerSystem.java
33 lines (29 loc) · 1.08 KB
/
DesignANumberContainerSystem.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
/*https://leetcode.com/problems/design-a-number-container-system/*/
class NumberContainers {
Map<Integer,TreeSet<Integer>> container;
Map<Integer,Integer> band;
public NumberContainers() {
container = new HashMap<Integer,TreeSet<Integer>>();
band = new HashMap<Integer,Integer>();
}
public void change(int index, int number) {
if (!container.containsKey(number))
container.put(number,new TreeSet<Integer>());
container.get(number).add(index);
if (!band.containsKey(index)) band.put(index,number);
else if (band.get(index) != number)
{
container.get((Integer)band.get(index)).remove(index);
band.put(index,number);
}
}
public int find(int number) {
return container.containsKey(number) && container.get(number).size() != 0 ? container.get(number).first() : -1;
}
}
/**
* Your NumberContainers object will be instantiated and called as such:
* NumberContainers obj = new NumberContainers();
* obj.change(index,number);
* int param_2 = obj.find(number);
*/