Failing on one of the test case


  • 0
    A
    Input:	2,[get(2),set(2,6),get(1),set(1,5),set(1,2),get(1),get(2)] 
    Output:	[-1,-1,2,-1]
    Expected:	[-1,-1,2,6]
    

    I created the cache with 2 items capacity, when I'm running set(1,2) item (2,6) became oldest item in the cache. Then cache is shrinked and item is removed from it. I though this is the expected cache behavior? Apparently, I miss something. Here is the trace from my program:

    get(2)
        -1
    set(2,6)
         debug cache content: 2:6
    get(1)
        -1
         debug cache content: 2:6
    set(1,5)
         debug cache content: 1:5 - > 2:6
    set(1,2)
         debug cache content: 1:2 - > 1:5
    get(1)
        2
         debug cache content: 1:2 - > 1:5
    get(2)
        -1
         debug cache content: 1:2 - > 1:5
    

    What do I do wrong?


  • 0
    K

    In my case, I got the same error because I forgot to add "else" after deciding if key is in cache or not. It could be possible.


  • 1
    G

    set(1, 5) only updates the value associating with the key 1. It doesn't add a new item in the cache.


  • 0
    A

    oh, now I got it, there is nothing saying it supposed to be unique. Thanks for clearing it up for me :)


Log in to reply
 

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