Skip to content

Commit d43e328

Browse files
committed
Create README - LeetHub
1 parent 65c15e0 commit d43e328

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

0146-lru-cache/README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<h2><a href="https://leetcode.com/problems/lru-cache/">146. LRU Cachea>h2><h3>Mediumh3><hr><p>Design a data structure that follows the constraints of a <strong><a href="https://en.wikipedia.org/wiki/Cache_replacement_policies#LRU" target="_blank">Least Recently Used (LRU) cachea>strong>.p>
2+
3+
<p>Implement the <code>LRUCachecode> class:p>
4+
5+
<ul>
6+
  • LRUCache(int capacity) Initialize the LRU cache with positive size capacity.
  • 7+
  • int get(int key) Return the value of the key if the key exists, otherwise return -1.
  • 8+
  • void put(int key, int value) Update the value of the key if the key exists. Otherwise, add the key-value pair to the cache. If the number of keys exceeds the capacity from this operation, evict the least recently used key.
  • 9+
    ul>
    10+
    11+
    <p>The functions <code>getcode> and <code>putcode> must each run in <code>O(1)code> average time complexity.p>
    12+
    13+
    <p>&nbsp;p>
    14+
    <p><strong class="example">Example 1:strong>p>
    15+
    16+
    <pre>
    17+
    <strong>Inputstrong>
    18+
    [&quot;LRUCache&quot;, &quot;put&quot;, &quot;put&quot;, &quot;get&quot;, &quot;put&quot;, &quot;get&quot;, &quot;put&quot;, &quot;get&quot;, &quot;get&quot;, &quot;get&quot;]
    19+
    [[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]]
    20+
    <strong>Outputstrong>
    21+
    [null, null, null, 1, null, -1, null, -1, 3, 4]
    22+
    23+
    <strong>Explanationstrong>
    24+
    LRUCache lRUCache = new LRUCache(2);
    25+
    lRUCache.put(1, 1); // cache is {1=1}
    26+
    lRUCache.put(2, 2); // cache is {1=1, 2=2}
    27+
    lRUCache.get(1); // return 1
    28+
    lRUCache.put(3, 3); // LRU key was 2, evicts key 2, cache is {1=1, 3=3}
    29+
    lRUCache.get(2); // returns -1 (not found)
    30+
    lRUCache.put(4, 4); // LRU key was 1, evicts key 1, cache is {4=4, 3=3}
    31+
    lRUCache.get(1); // return -1 (not found)
    32+
    lRUCache.get(3); // return 3
    33+
    lRUCache.get(4); // return 4
    34+
    pre>
    35+
    36+
    <p>&nbsp;p>
    37+
    <p><strong>Constraints:strong>p>
    38+
    39+
    <ul>
    40+
  • 1 <= capacity <= 3000
  • 41+
  • 0 <= key <= 104
  • 42+
  • 0 <= value <= 105
  • 43+
  • At most 2 * 105 calls will be made to get and put.
  • 44+
    ul>

    0 commit comments

    Comments
     (0)