-
Notifications
You must be signed in to change notification settings - Fork 1
/
All O`one Data Structure
68 lines (61 loc) · 1.97 KB
/
All O`one Data Structure
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
class AllOne {
/** Initialize your data structure here. */
Map<String, Integer> map = new HashMap<>();
public AllOne() {
}
/** Inserts a new key <Key> with value 1. Or increments an existing key by 1. */
public void inc(String key) {
map.put(key, map.getOrDefault(key, 0)+1);
}
/** Decrements an existing key by 1. If Key's value is 1, remove it from the data structure. */
public void dec(String key) {
if(map.containsKey(key) == false)
return;
map.put(key, map.get(key)-1);
if(map.get(key)==0){
map.remove(key);
}
}
/** Returns one of the keys with maximal value. */
public String getMaxKey() {
if(map.size() == 0){
return "";
}
String res = "";
int min = Integer.MIN_VALUE;
Iterator hmIterator = map.entrySet().iterator();
while (hmIterator.hasNext()) {
Map.Entry mapElement = (Map.Entry)hmIterator.next();
if((int)mapElement.getValue() >= min){
res = (String)mapElement.getKey();
min = (int)mapElement.getValue();
}
}
return res;
}
/** Returns one of the keys with Minimal value. */
public String getMinKey() {
if(map.size() == 0){
return "";
}
String res = "";
int min = Integer.MAX_VALUE;
Iterator hmIterator = map.entrySet().iterator();
while (hmIterator.hasNext()) {
Map.Entry mapElement = (Map.Entry)hmIterator.next();
if((int)mapElement.getValue() <= min){
res = (String)mapElement.getKey();
min = (int)mapElement.getValue();
}
}
return res;
}
}
/**
* Your AllOne object will be instantiated and called as such:
* AllOne obj = new AllOne();
* obj.inc(key);
* obj.dec(key);
* String param_3 = obj.getMaxKey();
* String param_4 = obj.getMinKey();
*/