Implement a data structure supporting the following operations:
Inc(Key) - Inserts a new key with value 1. Or increments an existing key by 1. Key is guaranteed to be a non-empty string.
Dec(Key) - If Key’s value is 1, remove it from the data structure. Otherwise decrements an existing key by 1. If the key does not exist, this function does nothing. Key is guaranteed to be a non-empty string.
GetMaxKey() - Returns one of the keys with maximal value. If no element exists, return an empty string “”.
GetMinKey() - Returns one of the keys with minimal value. If no element exists, return an empty string “”.
Challenge: Perform all these in O(1) time complexity.
思路:
这道题关键在于如何保存数据形式:
1、使用一个Hashmap保存当前存入的key的频次frequency
2、倒序使用一个Treemap保存当前frequency为某个值时的所有key有哪些,Treemap按照key排序。
因此在inc和dec时只需要正常的更新两个map就可以,在取最大最小的时候,直接用有序的treemap的第一个key和最后一个key就可以(分别对应最小的frequency和最大的frequency),然后再将数据取出来就好。
class AllOne {
TreeMap<Integer,HashSet<String>> reversedIndex;
HashMap<String,Integer> index;
/** Initialize your data structure here. */
public AllOne() {
this.reversedIndex = new TreeMap<>();
this.index = new HashMap<>();
}
/** Inserts a new key <Key> with value 1. Or increments an existing key by 1. */
public void inc(String key) {
if (this.index.containsKey(key) == false){
this.index.put(key,1);
if(this.reversedIndex.containsKey(1) == false)
this.reversedIndex.put(1,new HashSet<String>());
this.reversedIndex.get(1).add(key);
} else{
int currentCounts = this.index.get(key);
this.reversedIndex.get(currentCounts).remove(key);
// 这里必须要做remove,因为treemap要直接firstkey()或者lastkey,下面dec同理
if(this.reversedIndex.get(currentCounts).size() == 0){
this.reversedIndex.remove(currentCounts);
}
if(this.reversedIndex.containsKey(currentCounts + 1) == false){
this.reversedIndex.put(currentCounts + 1,new HashSet<>());
}
this.index.put(key,currentCounts + 1);
this.reversedIndex.get(currentCounts + 1).add(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(this.index.containsKey(key)){
int currentCounts = this.index.get(key);
this.reversedIndex.get(currentCounts).remove(key);
if(this.reversedIndex.get(currentCounts).size() == 0){
this.reversedIndex.remove(currentCounts);
}
if(currentCounts == 1){
this.index.remove(key);
} else{
if(this.reversedIndex.containsKey(currentCounts - 1) == false){
this.reversedIndex.put(currentCounts - 1,new HashSet<>());
}
this.reversedIndex.get(currentCounts -1).add(key);
this.index.put(key,currentCounts - 1);
}
}
}
/** Returns one of the keys with maximal value. */
public String getMaxKey() {
if (this.index.size() == 0 ) return "";
return this.reversedIndex.get(this.reversedIndex.lastKey()).iterator().next();
}
/** Returns one of the keys with Minimal value. */
public String getMinKey() {
if (this.index.size() == 0 ) return "";
return this.reversedIndex.get(this.reversedIndex.firstKey()).iterator().next();
}
}
/**
* 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();
*/