I don't understand why the expected output is what it is


  • 0
    Y

    Input: 2,[set(2,1),set(2,2),get(2),set(1,1),set(4,1),get(2)]
    Output: [1,-1]
    Expected: [2,-1]

    I don't understand why the expected output is this.

    In my understanding, the 2nd set(2,2) should not have any effect on the LRU values. This is because the instruction for the set function says: "set or insert if the key is not already present". So for the 1st get(2), the output is 1. After set(1,1) and set(4,1), the key 2 is no longer in the LRU. So 2nd get(2) produces -1.

    Thus, if my understanding is correct, the output should be [1,-1]. Is there anything I am not getting correctly?


  • 0
    S

    Maybe @1337 need to update the problem description to clarify it.

    I treated set as

    1. Set or insert the value if the key is not already present.

    2. Set a new value of the key which is already in cache.


  • 0
    G

    Yes, the description of set is not very obvious,
    but it is very intuitive that set should overwrite the previous value if key is present, otherwise insert the node with new key, value pair.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.