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();
 */
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章