Giter VIP home page Giter VIP logo

lru-cache's Introduction

LRU Cache

LRU Cache is a data structure that follows the constraints of a Least Recently Used (LRU) cache. It provides the ability to store key-value pairs and perform efficient operations such as retrieving the value for a given key and updating or inserting key-value pairs. The LRU Cache has a fixed capacity and evicts the least recently used items when the capacity is exceeded.

This implementation of the LRU Cache provides the following methods:

  • LRUCache(capacity): Initializes a new LRU Cache with the given capacity.
  • get(key): Returns the value associated with the given key if it exists in the cache, otherwise returns -1.
  • put(key, value): Updates the value for the given key if it exists in the cache. If the key does not exist, it adds the key-value pair to the cache. If the capacity is exceeded, it evicts the least recently used key-value pair.

Usage

# Create an LRU Cache with capacity 2
cache = LRUCache(2)

# Insert key-value pairs into the cache
cache.put(1, 1)
cache.put(2, 2)

# Retrieve values from the cache
value1 = cache.get(1)  # Returns 1
value2 = cache.get(2)  # Returns 2
value3 = cache.get(3)  # Returns -1 (key does not exist)

# Update the value for an existing key
cache.put(1, 10)

# Eviction may occur when capacity is exceeded
cache.put(3, 3)  # Evicts key 2 (least recently used)

# Retrieve values again
value1 = cache.get(1)  # Returns 10
value2 = cache.get(2)  # Returns -1 (evicted)
value3 = cache.get(3)  # Returns 3
Time Complexity
The get and put operations of the LRUCache class each run in O(1) average time complexity, providing efficient access to the cache.

Space Complexity
The space complexity of the LRUCache class is O(capacity) as it requires additional space to store the key-value pairs and maintain the linked list structure.

lru-cache's People

Contributors

husnainsaeed00 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.