Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
random-pick-with-blacklist.cpp 1.56 KB
一键复制 编辑 原始数据 按行查看 历史
kamyu 提交于 2018-07-08 13:49 +08:00 . Update random-pick-with-blacklist.cpp
// Time: ctor: O(b)
// pick: O(1)
// Space: O(b)
class Solution {
public:
Solution(int N, vector<int> blacklist) :
n_(N - blacklist.size()) {
unordered_set<int> whitelist;
for (int i = n_; i < N; ++i) {
whitelist.emplace(i);
}
for (const auto& black : blacklist) {
whitelist.erase(black);
}
auto white = whitelist.cbegin();
for (const auto& black : blacklist) {
if (black < n_) {
lookup_[black] = *(white++);
}
}
}
int pick() {
int index = rand() % n_;
return lookup_.count(index) ? lookup_[index] : index;
}
private:
int n_;
unordered_map<int, int> lookup_;
};
// Time: ctor: O(blogb)
// pick: O(logb)
// Space: O(b)
class Solution2 {
public:
Solution(int N, vector<int> blacklist) :
n_(N - blacklist.size()),
blacklist_(blacklist) {
sort(blacklist_.begin(), blacklist_.end());
}
int pick() {
int index = rand() % n_;
int left = 0, right = blacklist_.size() - 1;
while (left <= right) {
auto mid = left + (right - left) / 2;
if (index + mid < blacklist_[mid]) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return index + left;
}
private:
int n_;
vector<int> blacklist_;
};
/**
* Your Solution object will be instantiated and called as such:
* Solution obj = new Solution(N, blacklist);
* int param_1 = obj.pick();
*/
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助