Design a data structure that supports all following operations in average O(1) time.
注意事项
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.
样例
// 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();
思路
基本思路还是构建数组v来存放元素,这样可以实现随机等概率访问。执行insert(val)操作时,要把元素添加到v的末尾,同时构建数据结构来存放val对应的在v中的位置。执行remove(val)操作时,如果val的最大位置不在v的末尾,将val最大位置的值为v的末尾的值,为了保证取到val的最大位置,用最大堆来存放位置。
#ifndef C954_H
#define C954_H
#include<iostream>
#include<vector>
#include<unordered_map>
#include<queue>
#include<stdlib.h>
#include<algorithm>
using namespace std;
class RandomizedCollection {
public:
/** Initialize your data structure here. */
RandomizedCollection() {
}
/** Inserts a value to the collection. Returns true if the collection did not already contain the specified element. */
bool insert(int val) {
// write your code here
//将元素添加到v的尾部
//m[val]中添加其位置
bool res = m.find(val) == m.end();
v.push_back(val);
m[val].push(v.size() - 1);
return res;
}
/** Removes a value from the collection. Returns true if the collection contained the specified element. */
bool remove(int val) {
// write your code here
if (m.find(val) == m.end())
return false;
//当要去除的值val不在v的尾部,将val所在的最大位置用来存放v的尾部
//删除在m中v.back()的最大位置.将v.back()的新位置添加到m[v.back()]
//删除在m中val的最大位置,删除v的最后一个元素
if (val != v.back())
{
v[m[val].top()] = v.back();
m[v.back()].pop();
m[v.back()].push(m[val].top());
}
m[val].pop();
v.pop_back();
return true;
}
/** Get a random element from the collection. */
int getRandom() {
// write your code here
//利用rand()返回v中任意位置上的元素
if (v.empty())
return 0;
return v[rand() % v.size()];
}
unordered_map<int, priority_queue<int>> m;//存放元素的值和在v中的位置(从大到小)
vector<int> v;//为保证getRandom()得到的结果可能性相同,将元素存入vector中
};
/**
* 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();
*/
#endif