[Leetcode] LFU cache Most infrequently used page substitution buffers

Source: Internet
Author: User

Design and implement a data structure for Least frequently Used (LFU) cache. It should support the following operations: get and put .

get(key)-Get The value ('ll always be positive) of the key if the key exists in the cache, otherwise return-1.
put(key, value)-Set or insert the value if the key is not already present. When the cache is reaches its capacity, it should invalidate the least frequently used item before inserting a new item. For the purpose of this problem, when there is a tie (i.e., b or more keys, which has the same frequency), the least rece Ntly used key would be evicted.

Follow up:
Could do both operations in O (1) time complexity?

Example:

Lfucache cache = new Lfucache (2/* capacity *); Cache.put (1, 1); Cache.put (2, 2); Cache.get (1);       Returns 1cache.put (3, 3);    Evicts key 2cache.get (2);       Returns-1 (not found) Cache.get (3);       Returns 3.cache.put (4, 4);    Evicts key 1.cache.get (1);       Returns-1 (not found) Cache.get (3);       Returns 3cache.get (4);       Returns 4

S

[Leetcode] LFU cache Most infrequently used page substitution buffers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.