-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem_0381_RandomizedCollection.cc
77 lines (69 loc) · 1.58 KB
/
Problem_0381_RandomizedCollection.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <map>
#include <random>
#include <set>
#include <vector>
using namespace std;
class RandomizedCollection
{
public:
RandomizedCollection() {}
bool insert(int val)
{
arr.push_back(val);
map[val].emplace(arr.size() - 1);
return map[val].size() == 1;
}
// 删除 value 时,把最后一个元素移动到这个 index 上
bool remove(int val)
{
if (!map.count(val))
{
return false;
}
auto&& valSet = map[val];
int valIndex = *valSet.begin();
int endValue = arr.back();
if (val == endValue)
{
// fast path
// 最后一个元素跟当前相等,直接移除这项即可
valSet.erase(arr.size() - 1);
}
else
{
// slow path
auto&& endSet = map[endValue];
endSet.emplace(valIndex);
arr[valIndex] = endValue;
endSet.erase(arr.size() - 1);
valSet.erase(valIndex);
}
arr.pop_back();
if (valSet.empty())
{
map.erase(val);
}
return true;
}
int getRandom() { return arr[random(0, arr.size() - 1)]; }
int random(int min, int max)
{
random_device seed;
ranlux48 engine(seed());
uniform_int_distribution<> distrib(min, max);
int res = distrib(engine);
return res;
}
private:
// val,{index1, index2, ...}
map<int, set<int>> map;
// arr[index] = val;
vector<int> arr;
};
/**
* 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();
*/