• Leetcode 380. 常数时间插入、删除和获取随机元素


    1.题目描述

    设计一个支持在平均 时间复杂度 O(1) 下,执行以下操作的数据结构。

      1. insert(val):当元素 val 不存在时,向集合中插入该项。
      2. remove(val):元素 val 存在时,从集合中移除该项。
      3. getRandom:随机返回现有集合中的一项。每个元素应该有相同的概率被返回

    示例:

    // 初始化一个空的集合。
    RandomizedSet randomSet = new RandomizedSet();
    
    // 向集合中插入 1 。返回 true 表示 1 被成功地插入。
    randomSet.insert(1);
    
    // 返回 false ,表示集合中不存在 2 。
    randomSet.remove(2);
    
    // 向集合中插入 2 。返回 true 。集合现在包含 [1,2] 。
    randomSet.insert(2);
    
    // getRandom 应随机返回 1 或 2 。
    randomSet.getRandom();
    
    // 从集合中移除 1 ,返回 true 。集合现在包含 [2] 。
    randomSet.remove(1);
    
    // 2 已在集合中,所以返回 false 。
    randomSet.insert(2);
    
    // 由于 2 是集合中唯一的数字,getRandom 总是返回 2 。
    randomSet.getRandom();

    2.解题思路

    分析:题目的难点在于有delete操作的情况下,要保证getRandom( )等概率随机返回集合中的一个元素。

    一般地,题目的对时间复杂度的要求越高,都需要使用更多的辅助结构,以“空间换时间”。这里可以采用“两个哈希表”(多一个哈希表)或者“一个哈希表加一个数组”(多一个数组)。

    渐进思路

    (1)没有delete(val),只有insert(val)和getRandom( )操作的情况下,连续的插入元素键值对<key,index>,因为index在逻辑上是连续,因此getRandom()等概率随机返回集合中的一个元素很容易实现,rand() % index (0~index-1)即可;

    (2)有delete(val)操作,可以删除元素键值对之后,使得index不连续,中间有空洞,所以此时getRandom()产生的index可能正好是被删除的,导致时间复杂度超过O(1),所以delete(val)操作需要有一些限定条件,即保证每删除一个元素键值对之后,index个数减一,但是整体index在逻辑上是连续的。

             例如:0~5 ——> 0~4  ——> 0~3

    代码里关键部分有注释。

    class RandomizedSet {
    public:
        /** Initialize your data structure here. */
        
        //建立两个hash表,一个是<key,index>,另一个是<index,key>;
        RandomizedSet() {
           
        }
        
        /** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
        bool insert(int val) {
            //元素不存在时,插入
            if(keyIndexMap.count(val) == 0)
            {
                keyIndexMap[val] = size;
                indexKeyMap[size] = val;
                ++size;
                return true;
            }
            return false;
        }
        
        /** Removes a value from the set. Returns true if the set contained the specified element. */
        bool remove(int val) {
            ////每删除一个键值对,用最后的键值对填充该空位,保证整个index在逻辑上连续,size减一
            if(keyIndexMap.count(val) == 1)
            {
                int removeIndex = keyIndexMap[val];
                int lastIndex = --size;//若size=1000,表示0~999;这里是取最后一个index
                
                int lastKey = indexKeyMap[lastIndex];
                keyIndexMap[lastKey] = removeIndex;
                indexKeyMap[removeIndex] = lastKey;
                         
                keyIndexMap.erase(val);
                indexKeyMap.erase(lastIndex);//下标方式取val对应的值index
                
                return true;
            }
            return false;
        }
          
        
        /** Get a random element from the set. */
        int getRandom() {
            if (size == 0) {
                    return NULL;
                }
            //srand((unsigned)time(NULL));  //去掉srand(),保证稳定的产生随机序列
            int randomIndex = (int) (rand() % size); // 0 ~ size -1
            return indexKeyMap[randomIndex];
        }
        
    private:   
        map<int,int> keyIndexMap;
        map<int,int> indexKeyMap;
        int size = 0;
    };
    
    /**
     * Your RandomizedSet object will be instantiated and called as such:
     * RandomizedSet obj = new RandomizedSet();
     * bool param_1 = obj.insert(val);
     * bool param_2 = obj.remove(val);
     * int param_3 = obj.getRandom();
     */

    3.用时更少的范例

    这是Leetcode官网上C++完成此题提高的用时排名靠前的代码,这里与上面的解法差异就在于额外的辅助结构的选择,这里选的是在哈希表的基础上多增加一个数组,数组操作的时间复杂度和哈希表操作的时间复杂度均为O(1),但是数组时间复杂度O(1)的常数项更小,因此,这种解法效率更高。

    class RandomizedSet {
    public:
        /** Initialize your data structure here. */
        RandomizedSet() {
            
        }
        
        /** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
        bool insert(int val) {
            if (m.count(val)) return false;
            nums.push_back(val);
            m[val] = nums.size() - 1;
            return true;
        }
        
        /** Removes a value from the set. Returns true if the set contained the specified element. */
        bool remove(int val) {
            if (!m.count(val)) return false;
            int last = nums.back();
            m[last] = m[val];
            nums[m[val]] = last;
            nums.pop_back();
            m.erase(val);
            return true;
        }
        
        /** Get a random element from the set. */
        int getRandom() {
            return nums[rand() % nums.size()];
        }
    //private:
    //注释掉private,提高一点速度
        vector<int> nums;
        unordered_map<int, int> m;
    };
    
    /**
     * Your RandomizedSet object will be instantiated and called as such:
     * RandomizedSet obj = new RandomizedSet();
     * bool param_1 = obj.insert(val);
     * bool param_2 = obj.remove(val);
     * int param_3 = obj.getRandom();
     */
  • 相关阅读:
    Vmstat主要关注哪些数据?
    Swap是个什么东东?
    Buffers与cached啥区别
    做错的题目——关于构造器返回值
    做错的题目——this的指向
    JS判断一个数是否为质数
    数组扁平化
    JS实现快速排序
    正则实现千分符
    获取鼠标的当前位置
  • 原文地址:https://www.cnblogs.com/paulprayer/p/9927564.html
Copyright © 2020-2023  润新知