前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >LeetCode 0381 - Insert Delete GetRandom O(1) - Duplicates allowed

LeetCode 0381 - Insert Delete GetRandom O(1) - Duplicates allowed

作者头像
Reck Zhang
发布2021-08-11 11:43:32
2130
发布2021-08-11 11:43:32
举报
文章被收录于专栏:Reck Zhang

Insert Delete GetRandom O(1) - Duplicates allowed

Desicription

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

Note: Duplicate elements are allowed.

  1. insert(val): Inserts an item val to the collection.
  2. remove(val): Removes an item val from the collection if present.
  3. getRandom: Returns a random element from current collection of elements. The probability of each element being returned is linearly related to the number of same value the collection contains.

Example:

代码语言:javascript
复制
// Init an empty collection.
RandomizedCollection collection = new RandomizedCollection();

// Inserts 1 to the collection. Returns true as the collection did not contain 1.
collection.insert(1);

// Inserts another 1 to the collection. Returns false as the collection contained 1. Collection now contains [1,1].
collection.insert(1);

// Inserts 2 to the collection, returns true. Collection now contains [1,1,2].
collection.insert(2);

// getRandom should return 1 with the probability 2/3, and returns 2 with the probability 1/3.
collection.getRandom();

// Removes 1 from the collection, returns true. Collection now contains [1,2].
collection.remove(1);

// getRandom should return 1 and 2 both equally likely.
collection.getRandom();

Solution

代码语言:javascript
复制
class RandomizedCollection {
private:
    std::unordered_map<int, std::vector<int>> map = std::unordered_map<int, std::vector<int>>{};
    std::vector<std::pair<int, int>> vector = std::vector<std::pair<int, int>>{};
public:
    /** Initialize your data structure here. */
    RandomizedCollection() = default;

    /** Inserts a value to the collection. Returns true if the collection did not already contain the specified element. */
    bool insert(int val) {
        auto result = map.find(val) == map.end();
        map[val].push_back(vector.size());
        vector.push_back({val, map[val].size() - 1});

        return result;
    }

    /** Removes a value from the collection. Returns true if the collection contained the specified element. */
    bool remove(int val) {
        auto result = map.find(val) != map.end();

        if(result) {
            auto last = vector.back();
            map[last.first][last.second] = map[val].back();
            vector[map[val].back()] = last;
            map[val].pop_back();
            if(map[val].empty()) {
                map.erase(val);
            }
            vector.pop_back();
        }

        return result;
    }

    /** Get a random element from the collection. */
    int getRandom() {
        return vector[rand() % vector.size()].first;
    }
};

/**
 * Your RandomizedCollection object will be instantiated and called as such:
 * RandomizedCollection* obj = new RandomizedCollection();
 * bool param_1 = obj->insert(val);
 * bool param_2 = obj->remove(val);
 * int param_3 = obj->getRandom();
 */
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-07-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Insert Delete GetRandom O(1) - Duplicates allowed
    • Desicription
      • Solution
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档