Tensorum
YouTubeশেখা শুরু করুন
LeetCode 146 · Design

LRU Cache

LeetCode 146 · মাঝারি · সমাধান, intuition ও ধাপে ধাপে ওয়াকথ্রু

Hash map + doubly linked list with sentinels.

মাঝারিHash TableLinked ListDesignDoubly-Linked List
LeetCode-এ খুলুন ↗

আমি LRU Cache (LeetCode 146) এভাবে সমাধান করেছি — Design প্যাটার্নের একটি মাঝারি প্রবলেম। নিচে: intuition, ধাপে ধাপে approach, আমার accepted Python কোড, animated test cases আর line-by-line debugger।

নিচের বিবরণ ইংরেজিতে।

Intuition

You need O(1) lookup and O(1) 'move to most recent' / 'evict least recent'. A hash map finds nodes; a doubly linked list keeps recency order — the most recent sits after head, the least recent before tail.

Approach

  1. 1

    get: if present, move the node to the front and return its value.

  2. 2

    put: remove an existing node; if full, remove tail.prev.

  3. 3

    Insert the new node after head and record it in the map.

আমি কীভাবে সমাধান করেছি

Hash map + doubly linked listPythonO(1) per op
class Node:
    def __init__(self,key,val):
        self.key = key
        self.val = val
        self.prev=None
        self.next=None

class LRUCache:
    def __init__(self, capacity: int):
        self.capacity = capacity
        self.head = Node(0,0)
        self.tail = Node(0,0)
        self.head.next = self.tail
        self.tail.prev = self.head
        self.loc_mpp = {}


    def get(self, key: int) -> int:
        if key in self.loc_mpp:
            node = self.loc_mpp.get(key)
            self.remove(node)
            self.insert(node)
            return node.val
        return -1

    def put(self, key: int, value: int) -> None:
        if key in self.loc_mpp:
            node = self.loc_mpp[key]
            self.remove(node)
        if len(self.loc_mpp)==self.capacity:
            self.remove(self.tail.prev)
        node = Node(key,value)
        self.insert(node)

    def insert(self,new_node) -> None:
        #head - next node
        #head - new_node -> next_node
        self.loc_mpp[new_node.key]=new_node
        next_node = self.head.next
        new_node.next = next_node
        next_node.prev = new_node
        self.head.next = new_node
        new_node.prev=self.head
    def remove(self,old_node) -> None:
        del self.loc_mpp[old_node.key]
        #prev_node old_node next_node
        prev_node = old_node.prev
        next_node = old_node.next
        prev_node.next = next_node
        next_node.prev = prev_node
        


# Your LRUCache object will be instantiated and called as such:
# obj = LRUCache(capacity)
# param_1 = obj.get(key)
# obj.put(key,value)
স্টেপ-থ্রু লোড হচ্ছে…

LRU Cache — সাধারণ প্রশ্ন

LRU Cache-এর পেছনে intuition কী?

You need O(1) lookup and O(1) 'move to most recent' / 'evict least recent'. A hash map finds nodes; a doubly linked list keeps recency order — the most recent sits after head, the least recent before tail.

LRU Cache (LeetCode 146) কীভাবে সমাধান করবেন?

1. get: if present, move the node to the front and return its value. 2. put: remove an existing node; if full, remove tail.prev. 3. Insert the new node after head and record it in the map.

LRU Cache-এর time আর space complexity কত?

Time: O(1) per operation। Space: O(capacity)। (Accepted approach: Hash map + doubly linked list।)

LRU Cache-এ সাধারণ ভুলগুলো কী?

Sentinel head and tail remove all null checks. Store the key in the node so eviction can delete it from the map.