432. All O`one Data Structure(java)

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.

由于要达到O(1)的时间复杂度,只能用双向链表达到,注意技巧:用一个dummy head和一个dummy node可以简化max和min的返回,可以不用维护一个min pointer和一个max pointer。

class AllOne {
    Node head = new Node();
    Node tail = new Node();
    Map<String, Integer> map1;
    Map<Integer, Node> map2;
    /** Initialize your data structure here. */
    public AllOne() {
        head.next = tail;
        tail.pre = head;
        head.keys.add("");
        tail.keys.add("");
        map1 = new HashMap<>();
        map2 = new HashMap<>();
    }
    
    /** Inserts a new key <Key> with value 1. Or increments an existing key by 1. */
    public void inc(String key) {
        if (!map1.containsKey(key)) {
            map1.put(key, 1);
            if (!map2.containsKey(1)) {
                Node n = new Node();
                map2.put(1, n);
                insert(head, n);
            }
            map2.get(1).keys.add(key);
        } else {
            int count = map1.get(key);
            map1.put(key, count+1);
            Node pre = map2.get(count);
            pre.keys.remove(key);
            if (!map2.containsKey(count+1)) {
                Node n = new Node();
                map2.put(count+1, n);
                insert(pre, n);
            }
            map2.get(count+1).keys.add(key);
            if (pre.keys.size() == 0) {
                Node previous = pre.pre;
                previous.next = previous.next.next;
                previous.next.pre = previous;
                map2.remove(count);
            }
        }
    }
    
    /** Decrements an existing key by 1. If Key's value is 1, remove it from the data structure. */
    public void dec(String key) {
        if (map1.containsKey(key)) {
            int count = map1.get(key);
            Node next = map2.get(count);
            next.keys.remove(key);
            if (next.keys.size() == 0) {
                Node pre = next.pre;
                pre.next = pre.next.next;
                pre.next.pre = pre;
                map2.remove(count);
            }
            if (count == 1) {
                map1.remove(key);
            } else {
                if (!map2.containsKey(count-1)) {
                    Node n = new Node();
                    map2.put(count-1, n);
                    insert(next.pre, n);
                }
                map2.get(count-1).keys.add(key);
                map1.put(key, count-1);
            }
        }
    }
    
    public void insert(Node pre, Node next) {
        pre.next.pre = next;
        next.next = pre.next;
        pre.next = next;
        next.pre = pre;
    }
    
    /** Returns one of the keys with maximal value. */
    public String getMaxKey() {
        return tail.pre.keys.iterator().next();
    }
    
    /** Returns one of the keys with Minimal value. */
    public String getMinKey() {
        return head.next.keys.iterator().next();
    }
    
    class Node{
        Node pre;
        Node next;
        Set<String> keys;
        public Node() {
            keys = new HashSet<>();
        }
    }
}

/**
 * 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();
 */
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值