GithubHelp home page GithubHelp logo

lru-cache's Introduction

LRU Cache

Implementation :

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

class LRUCache {
    
    Map<Integer, ListNode> hashtable = new HashMap<Integer, ListNode>();
    ListNode head;
    ListNode tail;
    int totalItemsInCache;
    int maxCapacity;
    
    private class ListNode {
      int key;
      int value;
      ListNode prev;
      ListNode next;
    }

    public LRUCache(int capacity) {
      this.totalItemsInCache = 0;
      this.maxCapacity = capacity;
      head = new ListNode();
      tail = new ListNode();
      head.next = tail;
      tail.prev = head;
    }
    
    public int get(int key) {
      ListNode node = hashtable.get(key);
      if (node == null) 
         return -1;

      moveToHead(node);
      return node.value;  
    }
    
    private void moveToHead(ListNode node) {
      removeFromList(node);
      addToFront(node);
    }
    
    private void removeFromList(ListNode node) {
      ListNode savedPrev = node.prev;
      ListNode savedNext = node.next;

      savedPrev.next = savedNext;
      savedNext.prev = savedPrev;
    }
    
    private void addToFront(ListNode node) {
      // Wire up the new node being to be inserted
      node.prev = head;
      node.next = head.next;
      head.next.prev = node;
      head.next = node;
    }
    
    public void put(int key, int value) {
      ListNode node = hashtable.get(key);

      if (node == null) {
        ListNode newNode = new ListNode();
        newNode.key = key;
        newNode.value = value;
          
        hashtable.put(key, newNode);
        addToFront(newNode);
        totalItemsInCache++;

        if (totalItemsInCache > maxCapacity) {
          removeLRUEntry();
        }
      } else {
        node.value = value;
        moveToHead(node);
      }
    }
    
    private void removeLRUEntry() {
      ListNode tail = popTail();

      hashtable.remove(tail.key);
      --totalItemsInCache;
    }

    private ListNode popTail() {
      ListNode tailItem = tail.prev;
      removeFromList(tailItem);

      return tailItem;
    }
}

References :

  1. https://www.youtube.com/watch?v=S6IfqDXWa10
  2. https://github.com/bephrem1/backtobackswe/blob/master/Linked%20Lists/LRUCache/LRUCache.java

lru-cache's People

Contributors

emahtab avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.