381. Insert Delete GetRandom O(1) - Duplicates allowed
Design a data structure that supports all following operations in average O(1) time.
Note: Duplicate elements are allowed.
insert(val): Inserts an item val to the collection.remove(val): Removes an item val from the collection if present.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:
// 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();
Approach #1: C++.
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) {
auto result = m.find(val) == m.end();
m[val].push_back(nums.size());
nums.push_back(pair<int, int>(val, m[val].size() - 1));
return result;
}
/** Removes a value from the collection. Returns true if the collection contained the specified element. */
bool remove(int val) {
if (!m.count(val)) return false;
else {
auto last = nums.back();
m[last.first][last.second] = m[val].back();
nums[m[val].back()] = last;
m[val].pop_back();
if (m[val].empty()) m.erase(val);
nums.pop_back();
return true;
}
}
/** Get a random element from the collection. */
int getRandom() {
return nums[rand() % nums.size()].first;
}
private:
vector<pair<int, int>> nums;
unordered_map<int, vector<int>> m;
};
/**
* 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();
*/
In this solution we use vector<pair<int, int>> nums to resoter the numbers in the set, using the unordered_map<int, vector<int>> to restore the position of the number.
Runtime: 36 ms, faster than 82.83% of C++ online submissions for Insert Delete GetRandom O(1) - Duplicates allowed.
381. Insert Delete GetRandom O(1) - Duplicates allowed的更多相关文章
- leetcode 380. Insert Delete GetRandom O(1) 、381. Insert Delete GetRandom O(1) - Duplicates allowed
380. Insert Delete GetRandom O(1) 实现插入.删除.获得随机数功能,且时间复杂度都在O(1).实际上在插入.删除两个功能中都包含了查找功能,当然查找也必须是O(1). ...
- [LeetCode] 381. Insert Delete GetRandom O(1) - Duplicates allowed 常数时间内插入删除和获得随机数 - 允许重复
Design a data structure that supports all following operations in average O(1) time. Note: Duplicate ...
- [LeetCode] 381. Insert Delete GetRandom O(1) - Duplicates allowed 插入删除和获得随机数O(1)时间 - 允许重复
Design a data structure that supports all following operations in average O(1) time. Note: Duplicate ...
- LeetCode 381. Insert Delete GetRandom O(1) - Duplicates allowed
原题链接在这里:https://leetcode.com/problems/insert-delete-getrandom-o1-duplicates-allowed/?tab=Description ...
- LeetCode 381. Insert Delete GetRandom O(1) - Duplicates allowed O(1) 时间插入、删除和获取随机元素 - 允许重复(C++/Java)
题目: Design a data structure that supports all following operations in averageO(1) time. Note: Duplic ...
- LeetCode 381. Insert Delete GetRandom O(1) - Duplicates allowed (插入删除和获得随机数 常数时间 允许重复项)
Design a data structure that supports all following operations in average O(1) time. Note: Duplicate ...
- [leetcode]381. Insert Delete GetRandom O(1) - Duplicates allowed常数时间插入删除取随机值
Design a data structure that supports all following operations in average O(1) time. Note: Duplicate ...
- 381. Insert Delete GetRandom O(1) - Duplicates allowed允许重复的设计1数据结构
[抄题]: Design a data structure that supports all following operations in average O(1) time. Note: Dup ...
- 381 Insert Delete GetRandom O(1) - Duplicates allowed O(1) 时间插入、删除和获取随机元素 - 允许重复
设计一个支持在平均 时间复杂度 O(1) 下, 执行以下操作的数据结构.注意: 允许出现重复元素. insert(val):向集合中插入元素 val. remove(val):当 val ...
随机推荐
- VMware虚拟机下实现Linux与window文件夹共享
这里说的是在VMware虚拟机下来实现在windows与Linux下共享一个文件夹. 下面来说明一下是如何实现的: 1.安装VMware.Workstation. 2.安装Fedora10. 3.完成 ...
- 在OC项目中实现swift与oc混编 相互引用
--------------------------------------------------------Begin--------------------------------------- ...
- Java NIO 粘包 拆包 (实战) - 史上最全解读
疯狂创客圈 Java 聊天程序[ 亿级流量]实战系列之13 [博客园 总入口 ] 本文的源码工程:Netty 粘包/半包原理与拆包实战 源码 本实例是<Netty 粘包/半包原理与拆包实战> ...
- curl is a tool to transfer data from or to a server curl POST
https://curl.haxx.se/docs/manpage.html curl is a tool to transfer data from or to a server, using on ...
- TControl,TWinControl和TGraphicControl的显示函数
-------------------------- 显示隐藏刷新 -------------------------- TControl = class(TComponent)procedure S ...
- 异常描述:hibernate懒加载中,用OpenSessionInViewFilter解决之后,同时对一个collection创建两个session访问导致异常(Illegal attempt to associate a collection with two open sessions)
在保存的时候如果使用以下方法就会报错 解决:使用merge()方法就可以解决异常... merge()方法的解释: 传入的参数在数据库中不存在的时候会添加一条数据,根据主键判断已存在的时候会更新这条数 ...
- Java GET和POST请求
从表面来看GET和POST请求: GET请求是在url后直接附上请求体,url和请求体之间用"?"分割,不同参数之间用"&"分隔,%XX中的XX为该符号 ...
- Sublime Text 相关教程(转)
曾经有人说过,世界上有两种编辑器,好用和不好用的:而在好用的编辑器中,又分两种,免费的和死贵死贵的.譬如说VIM 和 TextMate,就是免费和死贵的典型.很不幸,今天的主角 Sublime Tex ...
- js日期的初始化的格式
js在初始化日期对象时,如果有传入日期.则格式有兼容性问题: //下面的写法在谷歌下没有问题,在火狐和ie下有问题var time = new Date('2014-11-27 00:00:00'); ...
- 关于「环境变量」PATH,CLASSPATH
以前在Windows中配置Java环境变量的时候初次遇到要配置环境变量,当时并不知道那是干什么用的,只知道配置了之后,在任何一个文件夹都可以使用"javac"命令来编译java文件 ...