Giter Site home page Giter Site logo

freddiehaddad / lrucache Goto Github PK

View Code? Open in Web Editor NEW
1.0 0.0 0.0 4 KB

Least recently used (LRU) in-memory cache.

Home Page: https://leetcode.com/problems/lru-cache/?envType=study-plan-v2&envId=top-interview-150

Go 100.00%
go golang leetcode leetcode-go leetcode-golang leetcode-solution lru-algorithm lru-cache lru-eviction lru-implementation

lrucache's Introduction

LRU Cache

Fully associative and in-memory least recently used (LRU) cache implementation.

The cache works by using two data structures:

  • Map
  • Linked List

Cache entries are fetched and stored in the map using the user-defined key.

Bookkeeping

The linked list is used for constant-time tracking of the least and most recently used entries. The head of the list serves as the least recently used object and the tail as most recently used.

For each Get operation that results in a cache hit, the element is moved to the end of the linked list to reflect it's most recently used status.

For Put operations, whether its a new entry being added or an update to an existing element, the entry is moved to the tail of the list.

Demonstration

LRUCache(3)

+-----+-----+-----+
|     |     |     |
+-----+-----+-----+

Put(1,1)

+-----+-----+-----+
|     |     | 1,1 |
+-----+-----+-----+

Put(2,2)

+-----+-----+-----+
|     | 1,1 | 2,2 |
+-----+-----+-----+

Put(3,3)

+-----+-----+-----+
| 1,1 | 2,2 | 3,3 |
+-----+-----+-----+

Get(1)

+-----+-----+-----+
| 2,2 | 3,3 | 1,1 |
+-----+-----+-----+

Put(3,4)

+-----+-----+-----+
| 2,2 | 1,1 | 3,4 |
+-----+-----+-----+

Put(5,5)

+-----+-----+-----+
| 1,1 | 3,4 | 5,5 |
+-----+-----+-----+

lrucache's People

Contributors

freddiehaddad avatar

Stargazers

 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.