Code Interview Note
  • 0. Introduction
  • 1. Basic
    • Python Basic
    • Java Basic
    • Primitive Type
    • Basic Question
    • Number
  • 2. Array and Numbers
    • General
    • TwoSum
    • Buy and Sell Stock
    • SubArray
      • SubArray + HashMap
    • Sliding Window
      • Sliding Window At Most Problem
    • Word Break
    • Passes Problem
    • Majority Element
    • Partition Array
    • Sort Colors
    • Anagram
    • Ugly Number
    • TwoPointer
    • Swipe Line
    • Calculator
    • Sudoku
  • 2.1 String
    • String
    • Palindrome
    • Parentheses
    • Decode String
    • Calculator
    • Abbreviation
  • 3. Linkedlist
    • Dummy Node
    • Double Pointers
  • 4. Stack and Queue
    • General
    • Increase/Decrease Stack
  • 5. Binary Search
    • General
    • BS on result
    • Save the half which has result
    • Rotated Sorted Array
    • Split Array Largest Sum
  • 6. Binary Tree
    • General
    • Path Sum
    • Lowest Common Ancestor
    • BST
    • Convert
    • Traverse
    • Valid Ordered Tree
    • Construct Binary Tree
    • Tree depth and width
    • Vertical Order Traverse
  • 7. Heap
    • Geneal
    • TopK
  • 8. Simulation
    • General
    • Read4
    • Encode Decode
    • LRU/LFU
    • Robot
    • GetRandom O(1)
    • Probability
  • 9. DFS
    • Backtrack
    • General
    • Subset
    • Permutation
    • Combination
  • 10. HashTable
    • General
  • 11. Sort
    • General
  • 12. Recursion
    • General
  • 13. Dynamic Programming
    • Graph
    • General
    • Coordinate
    • Double Sequence
    • Longest Common Subsequence
    • Rolling Array
    • House Robber
    • Backpack
    • Memorization
    • Diagonal
  • 14. BFS
    • General
    • Number of Islands
    • The Maze
  • 15. Graph
    • Shortest Path
    • Undirected Graph
    • Topology Sort
    • Word Ladder
    • Tarjan's Algo
  • 16. Divide & Conquer
    • General
  • 17. UnionFind
    • General
    • Grouping
  • 18. Trie
    • General
    • Word Square
  • 19. Company Summary
    • Oracle
    • Amazon
      • DP
    • Google
    • Hackerrank
    • LinkedIn
  • 20. Design
  • 21. Math
  • Behavior Question
  • Internet
  • OS
Powered by GitBook
On this page

Was this helpful?

  1. 10. HashTable

General

LRU cache

  1. Use HashMap and DoubleLinkedList,

    Put the latest item to the tail of DoubleLinkedList

public class LRUCache {

    private class Node{
        Node prev;
        Node next;
        int key;
        int val;

        public Node(int key, int val){
            this.key = key;
            this.val = val;
            prev = null;
            next = null;
        }
    }

    private int capacity;
    private HashMap<Integer, Node> cache = new HashMap<Integer, Node>();
    private Node head = new Node(-1, -1);
    private Node tail = new Node(-1, -1);
    public LRUCache(int capacity) {
        this.capacity = capacity;
        head.next = tail;
        tail.prev = head;
    }

    public int get(int key) {
        if (!cache.containsKey(key)){
            return -1;
        }

        // remove cur
        Node cur = cache.get(key);
        cur.prev.next = cur.next;
        cur.next.prev = cur.prev;

        // move cur to tail        
        moveToTail(cur);
        return cache.get(key).val;
    }

    public void set(int key, int value) {
        if (get(key) != -1){
            cache.get(key).val = value;
            return;
        }

        if (cache.size() == capacity){
            cache.remove(head.next.key);
            head.next = head.next.next;
            head.next.prev = head;
        }

        Node insert = new Node(key, value);
        cache.put(key, insert);
        moveToTail(insert);
    }

    private void moveToTail(Node cur){
        cur.prev = tail.prev;
        cur.next = tail;
        tail.prev = cur;
        cur.prev.next = cur;
    }
}
  1. Use the LinkedHashMap

    • LinkedHashMap(int initialCapacity, float loadFactor, boolean accessOrder)

      If access order, the latest entry will be updated Override the removeEldestEntry method

public class LruCache extends LinkedHashMap{

  private int capacity;

  LruCache(final int capacity) {n
    super(capacity, 1, true); // true if access order, false if insertion order
    this.capacity = capacity;
  };

  @Override
  protected boolean removeEldestEntry(Map.Entry e) {
    return this.size() > capacity;
  }

  public Integer lookup(Integer key) {
    if (this.containsKey(key)){
      return (Integer)this.get(key);
    }else{
      return null;
    }
  }

  public void insert(Integer key, Integer value) {
    if (!this.containsKey(key)){
      this.put(key, value);
    }
  }

  public Boolean erase(Object key) {
    if (this.containsKey(key)){
      this.remove(key);
      return true;
    }else{
      return false;
    }
  }
}
Previous10. HashTableNext11. Sort

Last updated 5 years ago

Was this helpful?