Design and implement a data structure for Least Frequently Used (LFU) cache. It should support the following operations: get and set.

get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1. set(key, value) - Set or insert the value if the key is not already present. When the cache reaches its capacity, it should invalidate the least frequently used item before inserting a new item. For the purpose of this problem, when there is a tie (i.e., two or more keys that have the same frequency), the least recently used key would be evicted.

Follow up: Could you do both operations in O(1) time complexity?

Example:

LFUCache cache = new LFUCache( 2 /* capacity */ );

cache.set(1, 1);
cache.set(2, 2);
cache.get(1);       // returns 1
cache.set(3, 3);    // evicts key 2
cache.get(2);       // returns -1 (not found)
cache.get(3);       // returns 3.
cache.set(4, 4);    // evicts key 1.
cache.get(1);       // returns -1 (not found)
cache.get(3);       // returns 3
cache.get(4);       // returns 4

Solution

public class LFUCache {

    public class Node {
        public Set<Integer> keys;
        public Node prev;
        public Node next;
        public int freq;
        public Node(Node prev, Node next, int freq) {
            this.prev = prev;
            this.next = next;
            this.freq = freq;
            keys = new LinkedHashSet<Integer>();
        }
    }

    int cap = 0;
    int usage = 0;
    Map<Integer, Integer> content = new HashMap<Integer, Integer>();
    Map<Integer, Node> place = new HashMap<Integer, Node>();
    Node head = new Node(null, null, 0);
    Node tail = new Node(null, null, 0);

    public LFUCache(int capacity) {
        cap = capacity;
        head.next = tail;
        tail.prev = head;
    }

    public int get(int key) {
        if(cap == 0) return -1;

        if(content.containsKey(key)) {
            increaseFreq(key);
            return content.get(key);
        } else {
            return -1;   
        }
    }

    public void set(int key, int value) {
        if(cap == 0) return;

        if(content.containsKey(key)) {
            increaseFreq(key);
            content.put(key, value);
        } else {
            // check usage
            if(usage == cap) {
                removeLeastFrequentKey();
            }
            createNewKey(key, value);
        }
    }


    public void increaseFreq(int key) {
        Node mynode = place.get(key);   

        if(mynode.next.freq != mynode.freq + 1) insertAfter(mynode);
        Node nextNode = mynode.next;
        nextNode.keys.add(key);

        mynode.keys.remove(key);
        if(mynode.keys.size() == 0) deleteNode(mynode);

        place.put(key, nextNode);
    }

    public void removeLeastFrequentKey() {
        usage--;
        int removed = head.next.keys.iterator().next();
        place.remove(removed);
        content.remove(removed);
        head.next.keys.remove(removed);

        if(head.next.keys.size() == 0) deleteNode(head.next);
    }

    public void createNewKey(int key, int value) {
        usage++;
        content.put(key, value);
        if(head.next.freq != 1) insertAfter(head);
        head.next.keys.add(key);

        place.put(key, head.next);
    }

    public Node insertAfter(Node node) {
        Node mid = new Node(node, node.next, node.freq + 1);
        node.next.prev = mid;
        node.next = mid;
        return mid;
    }

    public void deleteNode(Node node) {
        Node prev = node.prev;
        Node next = node.next;
        prev.next = next;
        next.prev = prev;
    }
}

/**
 * Your LFUCache object will be instantiated and called as such:
 * LFUCache obj = new LFUCache(capacity);
 * int param_1 = obj.get(key);
 * obj.set(key,value);
 */

results matching ""

    No results matching ""