Basically no need to think about get()


  • -1
    S

    Hey, guys!

    I just wanted to notice that, actually, you need to think only about set() method because get() could be easily expressed through set(). If set() is O(1) then get() will also be O(1).

    An illustration of my words in Java code is below.

    public int get(int key) {
        // I used internal map m for storing data, 
        // so I just pick existing value from there,
        // but you can do the same with any DS you use internally
        int val = m.containsKey(key) ? m.get(key).val : -1;
        if (val != -1) set(key, val);
        return val;
    }
    

    Have a nice coding!


Log in to reply
 

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