成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專(zhuān)欄INFORMATION COLUMN

Insert Delete GetRandom O(1) & Duplicates allo

2shou / 3304人閱讀

摘要:思路可以實(shí)現(xiàn)時(shí)間復(fù)雜度的和,但是要求也是,只用是不可以的。但是在里面查找的時(shí)間復(fù)雜度依然是,可以想到用來(lái)記錄對(duì)應(yīng)的,這樣查找的時(shí)間也是常數(shù)。用可以保持順序,但是的時(shí)間復(fù)雜度是。

380. Insert Delete GetRandom O(1)

Design a data structure that supports all following operations in average O(1) time.

insert(val): Inserts an item val to the set if not already present.

remove(val): Removes an item val from the set if present.

getRandom: Returns a random element from current set of elements. Each element must have the same probability of being returned.

思路

HashSet可以實(shí)現(xiàn)O(1)時(shí)間復(fù)雜度的insert和remove,但是要求getRandom也是O(1),只用HashSet是不可以的。在List里面隨機(jī)找一個(gè)數(shù)的時(shí)間是O(1),所以可以用list來(lái)記錄加進(jìn)去的value,這樣insert和getRandom都是O(1)了。remove的操作比較有意思,在一個(gè)list里面找到相應(yīng)的值之后和最后一個(gè)位置上的值調(diào)換,這個(gè)remove就是常數(shù)時(shí)間了。但是在list里面查找的時(shí)間復(fù)雜度依然是O(N),可以想到用hashmap來(lái)記錄對(duì)應(yīng)的index,這樣查找的時(shí)間也是常數(shù)。

復(fù)雜度

Time: O(1), Space: O(N)

代碼
    Map map;
    List list;
    Random random;
    public RandomizedSet() {
        // hashmap to realize insert, remove in O(1), the value is the index in list
        map = new HashMap();
        // list to realize get random in O(1);
        list = new ArrayList();
        random = new Random();
    }
    
    /** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
    public boolean insert(int val) {
        if(map.containsKey(val)) return false;
        map.put(val, list.size());
        list.add(val);
        return true;
    }
    
    /** Removes a value from the set. Returns true if the set contained the specified element. */
    public boolean remove(int val) {
        // change the val with the last one, then delete last one
        if(!map.containsKey(val)) return false;
        int delete_index = map.get(val);
        int last = list.get(list.size() - 1);
        // change the last element to the delete place
        list.set(delete_index, last);
        // update the index in map
        map.replace(last, delete_index);
        // remove in the list
        list.remove((int) list.size() - 1);
        // remove in the map
        map.remove(val);
        return true;
    }
    
    /** Get a random element from the set. */
    public int getRandom() {
        if(list.size() == 0) return -1;
        return list.get(random.nextInt(list.size()));
    }
381. Insert Delete GetRandom O(1) - Duplicates allowed 思路

允許duplication之后麻煩了許多?,F(xiàn)在hashmap里面要存所有的index。在remove的時(shí)候復(fù)雜度要保持O(1),每次還是要取最后一個(gè)值和刪除的值交換,所以需要map里面找到最后一個(gè)值,之后刪除最大的index的時(shí)間復(fù)雜度是O(1),同時(shí)把index更新成當(dāng)前刪除的值的index之后,所有的index還要保持順序。

heap

用heap可以保持順序,但是poll的時(shí)間復(fù)雜度是O(logN)。

代碼
Map> map;
    List list;
    Random random;
    public RandomizedCollection() {
        map = new HashMap();
        list = new ArrayList();
        random = new Random();
    }
    
    /** Inserts a value to the collection. Returns true if the collection did not already contain the specified element. */
    public boolean insert(int val) {
        if(map.containsKey(val)) {
            map.get(val).add(list.size());
            list.add(val);
            return false;
        }
        else {
            map.put(val, new PriorityQueue<>((a, b) -> b - a));
            map.get(val).add(list.size());
            list.add(val);
            return true;
        }
    }
    
    /** Removes a value from the collection. Returns true if the collection contained the specified element. */
    public boolean remove(int val) {
        if(!map.containsKey(val)) return false;
        int delete_index = map.get(val).peek();
        int last = list.get(list.size() - 1);
        // update
        list.set(delete_index, last);
        map.get(last).poll();
        map.get(last).add(delete_index);
        // delete
        list.remove(list.size() - 1);
        map.get(val).poll();
        if(map.get(val).size() == 0) map.remove(val);
        return true;
    }
    
    /** Get a random element from the collection. */
    public int getRandom() {
        return list.get(random.nextInt(list.size()));
    }
LinkedHashSet

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/66477.html

相關(guān)文章

  • leetcode380. Insert Delete GetRandom O(1)

    摘要:題目要求設(shè)計(jì)一個(gè)數(shù)據(jù)結(jié)構(gòu),使得能夠在的時(shí)間復(fù)雜度中插入數(shù)字,刪除數(shù)字,以及隨機(jī)獲取一個(gè)數(shù)字。因此,使用來(lái)查詢時(shí)不可避免的。如何實(shí)現(xiàn)的隨機(jī)查詢這個(gè)其實(shí)就是強(qiáng)調(diào)一點(diǎn),我們需要維持原有的插入順序,從而保證各個(gè)元素等概率被隨機(jī)。 題目要求 Design a data structure that supports all following operations in average O(1)...

    phoenixsky 評(píng)論0 收藏0
  • leetcode381. Insert Delete GetRandom O(1) - Duplic

    摘要:題目要求設(shè)計(jì)一個(gè)數(shù)據(jù)結(jié)構(gòu),支持能夠在的時(shí)間內(nèi)完成對(duì)數(shù)字的插入,刪除和獲取隨機(jī)數(shù)的操作,允許插入重復(fù)的數(shù)字,同時(shí)要求每個(gè)數(shù)字被隨機(jī)獲取的概率和該數(shù)字當(dāng)前在數(shù)據(jù)結(jié)構(gòu)中的個(gè)數(shù)成正比。網(wǎng)上有一些實(shí)現(xiàn)采用來(lái)解決,這是不合理的。此時(shí)的代碼如下 題目要求 Design a data structure that supports all following operations in average...

    h9911 評(píng)論0 收藏0
  • 哈希函數(shù)與哈希表

    摘要:哈希函數(shù)與哈希表一哈希函數(shù)哈希函數(shù)性質(zhì)輸入域是無(wú)窮的輸出域有窮的當(dāng)輸入?yún)?shù)固定的情況下,返回值一定一樣當(dāng)輸入不一樣,可能得到一樣的值。 哈希函數(shù)與哈希表 一、哈希函數(shù) 1.1 哈希函數(shù)性質(zhì): input輸入域是無(wú)窮的 output輸出域有窮的 當(dāng)輸入?yún)?shù)固定的情況下,返回值一定一樣 當(dāng)輸入不一樣,可能得到一樣的值。(必然會(huì)出現(xiàn),因?yàn)檩斎胗蚝艽?,輸出域很?,產(chǎn)生哈希碰撞 均勻分布的特...

    Rainie 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<