[LeetCode] All O`one Data Structure

Implement a data structure supporting the following operations:

  1. Inc(Key) - Inserts a new key with value 1. Or increments an existing key by 1. Key is guaranteed to be a non-emptystring.
  2. 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.
  3. GetMaxKey() - Returns one of the keys with maximal value. If no element exists, return an empty string "".
  4. 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();
 */


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值