Tensorum
YouTubeಕಲಿಯಲು ಪ್ರಾರಂಭಿಸಿ
LeetCode 460 · Design

LFU Cache

LeetCode 460 · ಕಠಿಣ · ಪರಿಹಾರ, intuition ಮತ್ತು ಹಂತ ಹಂತದ ವಾಕ್‌ಥ್ರೂ

freq → OrderedDict buckets + min_freq.

ಕಠಿಣHash TableLinked ListDesignDoubly-Linked List
LeetCode ನಲ್ಲಿ ತೆರೆಯಿರಿ ↗

ನಾನು LFU Cache (LeetCode 460) ಅನ್ನು ಹೀಗೆ ಪರಿಹರಿಸಿದೆ — Design ಪ್ಯಾಟರ್ನ್‌ನ ಒಂದು ಕಠಿಣ ಪ್ರಾಬ್ಲಮ್. ಕೆಳಗೆ: intuition, ಹಂತ ಹಂತದ approach, ನನ್ನ accepted Python ಕೋಡ್, animated test cases ಮತ್ತು line-by-line debugger.

ಕೆಳಗಿನ ವಿವರಣೆ ಇಂಗ್ಲಿಷ್‌ನಲ್ಲಿದೆ.

Intuition

Track three maps: key → value, key → frequency, and frequency → ordered keys (oldest first). Remember the smallest frequency in use; eviction takes the oldest key at that frequency.

Approach

  1. 1

    get: move the key from bucket f to f+1; bump min_freq if bucket f emptied.

  2. 2

    put existing: run get, then update the value.

  3. 3

    put new: if full, pop the oldest key in the min_freq bucket.

  4. 4

    Insert with frequency 1 and set min_freq = 1.

ನಾನು ಹೇಗೆ ಪರಿಹರಿಸಿದೆ

Frequency bucketsPythonO(1) per op
class LFUCache:
    """
    #key2val map
    #key2freq map
    #freq2keys map 
    #maintain minfreq
    """
    def __init__(self, capacity: int):
        self.key2val = {}
        self.key2freq = {}
        self.freq2keys= defaultdict(collections.OrderedDict)
        self.min_freq =0
        self.capacity=capacity
    def get(self, key: int) -> int:
        #increase frequency if exists
        if key in self.key2val:
            old_freq = self.key2freq[key]
            self.freq2keys[old_freq].pop(key)
            #remove tombstone
            if not self.freq2keys[old_freq]:
                del self.freq2keys[old_freq]
            self.key2freq[key]=old_freq+1
            self.freq2keys[old_freq+1][key]=1
            if self.min_freq not in self.freq2keys:
                self.min_freq+=1
            return self.key2val[key]
        return -1

    def put(self, key: int, value: int) -> None:
        if self.capacity<=0:
            return
        #if key already exists in map - increase the frequency
        if key in self.key2val:
            self.get(key)
            self.key2val[key]=value
            return 
        if len(self.key2val)==self.capacity:
            delkey,_ = self.freq2keys[self.min_freq].popitem(last=False)
            del self.key2val[delkey]
            del self.key2freq[delkey]
        
        #latest item added
        self.min_freq=1
        self.key2val[key]=value
        self.key2freq[key]=1
        self.freq2keys[1][key]=1

# Your LFUCache object will be instantiated and called as such:
# obj = LFUCache(capacity)
# param_1 = obj.get(key)
# obj.put(key,value)
ಸ್ಟೆಪ್-ಥ್ರೂ ಲೋಡ್ ಆಗುತ್ತಿದೆ…

LFU Cache — ಪದೇ ಪದೇ ಕೇಳುವ ಪ್ರಶ್ನೆಗಳು

LFU Cache ಹಿಂದಿನ intuition ಏನು?

Track three maps: key → value, key → frequency, and frequency → ordered keys (oldest first). Remember the smallest frequency in use; eviction takes the oldest key at that frequency.

LFU Cache (LeetCode 460) ಅನ್ನು ಹೇಗೆ ಪರಿಹರಿಸುವುದು?

1. get: move the key from bucket f to f+1; bump min_freq if bucket f emptied. 2. put existing: run get, then update the value. 3. put new: if full, pop the oldest key in the min_freq bucket. 4. Insert with frequency 1 and set min_freq = 1.

LFU Cache ನ time ಮತ್ತು space complexity ಎಷ್ಟು?

Time: O(1) per operation. Space: O(capacity). (Accepted approach: Frequency buckets.)

LFU Cache ನಲ್ಲಿ ಸಾಮಾನ್ಯ ತಪ್ಪುಗಳು ಯಾವುವು?

Ties at the same frequency are broken by recency — hence the ordered buckets.