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-emptystring.
- 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.
由出题的思想可知,想要完成这样的数据结构,一定要可以通过String(Key)定位到他的times 也要能从times定位到String(key) ,所以需要两个数据结构分别记录二者的对应关系,其实就是用两个数据结构表示一个一对多的关系,本题使用TreeMap和HashMap 完成,并不是完全的o(1)时间复杂度完成inc()和desc()方法。
如果想要在o(1)时间复杂度内完成inc()和desc()方法,可以使用双向链表LikedList<Node>和HashMap<String,Integer>完成对应关系,Node中包含
class Node{
int times;
HashSet<String> set=new HashSet<String>();
public Node(int times) {
this.times = times;
}
}
这些信息,由于之前的代码丢失,这里只附上第一种方式实现的代码:
public class AllOne {
TreeMap<Integer,HashSet<String>> treeMap;
HashMap<String,Integer> hashMap;
/** Initialize your data structure here. */
public AllOne() {
treeMap=new TreeMap<Integer,HashSet<String>>();
hashMap=new HashMap<String, Integer>();
}
/** Inserts a new key <Key> with value 1. Or increments an existing key by 1. */
public void inc(String key) {
if(hashMap.containsKey(key)){
int num=hashMap.get(key);
hashMap.put(key,num+1);
treeMap.get(num).remove(key);
if(treeMap.get(num).isEmpty()) treeMap.remove(num);
if(treeMap.get(num+1)==null){
treeMap.put(num+1, new HashSet<String>());
}
treeMap.get(num+1).add(key);
}else{
hashMap.put(key,1);
if(treeMap.get(1)==null){
treeMap.put(1, new HashSet<String>());
}
treeMap.get(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(!hashMap.containsKey(key)) return;
int num=hashMap.get(key);
HashSet<String> set=treeMap.get(num);
set.remove(key);
if(set.isEmpty()) treeMap.remove(num);
if(num<=1){
hashMap.remove(key);
}else{
hashMap.put(key, num-1);
if(treeMap.get(num-1)==null){
treeMap.put(num-1, new HashSet<String>());
}
treeMap.get(num-1).add(key);
}
}
/** Returns one of the keys with maximal value. */
public String getMaxKey() {
if(treeMap.isEmpty()) return "";
else{
HashSet<String> set=treeMap.lastEntry().getValue();
Iterator<String> it=set.iterator();
return it.next();
}
}
/** Returns one of the keys with Minimal value. */
public String getMinKey() {
if(treeMap.isEmpty()) return "";
else{
HashSet<String> set=treeMap.firstEntry().getValue();
Iterator<String> it=set.iterator();
return it.next();
}
}
public static void main(String[] args) {
AllOne s=new AllOne();
s.inc("hello");
s.inc("hello");
System.out.println(s.hashMap);
System.out.println(s.treeMap);
}
}
/**
* 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();
*/