What is the answer of Input: 2,[set(2,1),set(1,1),get(2),set(4,1),get(1),get(2)] ????

  • 2

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

    I thought 2,1 would be the LRU and should be removed when set(4,1).
    so get(1) should be fine but get(2) should be -1.

    Did I misunderstand LRU??

  • 0

    After set(2,1),set(1,1),get(2), the LRU is (1,1) because you just used get(2), so the next step set(4,1) will remove (1,1). Therefore, get(1) return -1, get(2) return 1.

  • -1

    When get() is called, the priority of this number in cache if it exists in the cache is promoted.
    Meanwhile, the set() will not promote the priority of number in cache.
    I think above is what "LRU" actually means.

  • 0

    Set also need promote, used means "accessed", include both operation of get() and set(). At least in leetcode that's the rule, I'm not sure how the real operaion system define it :)

Log in to reply

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