Wrong expected output?

  • 0


    In test number 4 i get the following error:

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

    Output: [1,-1]

    Expected: [2,-1]

    Based in the information of set method:
    set(key, value) - Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item.

    If we have to avoid setting the new value if the key already exist, then the "set(2,2)" should be ignored and the output would be [1,-1].

    Or maybe the set method explanation needs to be more clarifying.

  • 0

    You are close, but are interpreting the instructions incorrectly.

    Read it like this:

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

    You do not ignore it, but it differs in that if the value is present, you do not insert another element with the same key; you just replace the object that key maps to. However, if it isn't present, then you obviously can't set the object for that key, since the key doesn't exist. Therefore, you must insert it.

Log in to reply

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