-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAllOne.java
51 lines (44 loc) · 1.41 KB
/
AllOne.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
public class AllOne {
Map<String, Integer> map;
int maxValue, minValue;
String maxKey, minKey;
/** Initialize your data structure here. */
public AllOne() {
map = new HashMap<>();
maxValue = Integer.MIN_VALUE;
minValue = Integer.MAX_VALUE;
}
/** Inserts a new key <Key> with value 1. Or increments an existing key by 1. */
public void inc(String key) {
if (!map.containsKey(key)) map.put(key, 1);
else map.put(key, map.get(key)+1);
if (map.get(key) > maxValue) {
maxValue = map.get(key);
maxKey = key;
}
}
/** 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)) return;
else map.put(key, map.get(key)-1);
if (map.get(key) == 0) map.remove(key);
if (map.get(key) < minValue) {
maxValue = map.get(key);
maxKey = key;
}
}
/** Returns one of the keys with maximal value. */
public String getMaxKey() {
}
/** Returns one of the keys with Minimal value. */
public String getMinKey() {
}
}
/**
* 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();
*/