LeetCode 381. Insert Delete GetRandom O(1) - Duplicates allowed (插入删除和获得随机数 常数时间 允许重复项)
2017-09-20 02:31
405 查看
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();
题目标签:Array, Hash Table, Design 这道题目基于#380 的情况下,可以允许有重复项。回顾一下380, 因为要达到 insert, remove 和 getRandom 都是O(1) 的时间,我们需要ArrayList 来保存所有的数字,利用map 来保存 数字 和 index 之间的映射。 这道题目允许了重复项,那么在map 里 key 是数字, value 是index, 这里的index 就会不止一个了。我们要把所有重复的数字的 index 也保存进来, 所以把 map 里value 改成 HashSet 来保存所有的index。 insert val:如果map里没有val,需要新建一个HashSet,并且加入nums (ArrayList); 如果有val,直接加入新的index 进HashSet,并且加入nums (ArrayList); remove val:如果val 在nums 里是最后一个的话,只需要在map 里删除val 的index, 并且在nums 里删除最后一个数字。 如果val 在nums 里不是最后一个的话,需要额外的把 nums 里最后一个数字的值 复制到 val, 删除val 在map里的 index,还要把最后一个数字的index 在map 里更新,并且在nums 里删除最后一个数字。 其他基本都和#380 差不多,具体看code。
Java Solution:
Runtime beats 88.45%
完成日期:09/18/2017
关键词:Array, Hash Table, Design
关键点:利用array 保存数值;利用map<Integer, HashSet<>>保存 - 数值 当作key,数值在array里的所有index 保存在HashSet,当作value。
class RandomizedCollection { private HashMap<Integer, HashSet<Integer>> map; // key is value, value is index HashSet private ArrayList<Integer> nums; // store all vals private java.util.Random rand = new java.util.Random(); /** Initialize your data structure here. */ public RandomizedCollection() { map = new HashMap<>(); nums = new ArrayList<>(); } /** Inserts a value to the collection. Returns true if the collection did not already contain the specified element. */ public boolean insert(int val) { boolean contain = map.containsKey(val); // if map doesn't have val, meaning map doesn't have HashSet if(!contain) map.put(val, new HashSet<Integer>()); // create HashSet // add index into HashSet map.get(val).add(nums.size()); nums.add(val); return !contain; // if collection has val, return false; else return true } /** Removes a value from the collection. Returns true if the collection contained the specified element. */ public boolean remove(int val) { boolean contain = map.containsKey(val); if(!contain) return false; // get an index from HashSet of Map int valIndex = map.get(val).iterator().next(); map.get(val).remove(valIndex); // remove this index from val's set if(valIndex != nums.size() - 1) // if this val is not the last one in nums { // copy the last one value into this val's position int lastNum = nums.get(nums.size() - 1); nums.set(valIndex, lastNum); // update the lastNum index in HashSet map.get(lastNum).remove(nums.size() - 1); // remove the last number's index from set map.get(lastNum).add(valIndex); // add new index into set } if(map.get(val).isEmpty()) // if val's set is empty map.remove(val); // remove val from map nums.remove(nums.size() - 1); // only remove last one O(1) return true; } /** Get a random element from the collection. */ public int getRandom() { return nums.get(rand.nextInt(nums.size())); } } /** * Your RandomizedCollection object will be instantiated and called as such: * RandomizedCollection obj = new RandomizedCollection(); * boolean param_1 = obj.insert(val); * boolean param_2 = obj.remove(val); * int param_3 = obj.getRandom(); */
参考资料:
https://discuss.leetcode.com/topic/53216/java-solution-using-a-hashmap-and-an-arraylist-along-with-a-follow-up-131-ms/5
LeetCode 题目列表 - LeetCode Questions List
相关文章推荐
- [LeetCode] Insert Delete GetRandom O(1) - Duplicates allowed 常数时间内插入删除和获得随机数 - 允许重复
- [LeetCode] 381. Insert Delete GetRandom O(1) - Duplicates allowed 插入删除和获得随机数O(1)时间 - 允许重复
- [LeetCode] Insert Delete GetRandom O(1) 常数时间内插入删除和获得随机数
- LeetCode 380. Insert Delete GetRandom O(1) (插入删除和获得随机数 常数时间)
- 380.Insert Delete GetRandom O(1) 常数时间内插入删除和获得随机数
- [LeetCode] 380. Insert Delete GetRandom O(1) 插入删除获得随机数O(1)时间
- Leetcode 381. Insert Delete GetRandom O(1) - Duplicates allowed 可重复常数增删改系统 解题报告
- [LeetCode] Remove Duplicates from Sorted Array 从有序数组中删除重复项
- leetcode之Insert Delete GetRandom O(1) - Duplicates allowed
- Leetcode Remove Duplicates from Sorted List II 删除链表中重复的元素II
- LeetCode | Remove Duplicates from Sorted List II(删除链表中重复结点2)
- leetcode | Delete Duplicate Emails | 删除表中重复项
- LeetCode 381---Insert Delete GetRandom O(1) - Duplicates allowed
- leetcode:Remove Duplicates from Sorted Array(去掉数组重复数字,常数空间限制)【面试算法题】
- LeetCode-Sort List,链表排序(插入和归并),时间复杂度O(n^2) and O(nlgn)
- [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
- [LeetCode] Find All Duplicates in an Array 找出数组中所有重复项
- 获得以当前时间为种子的随机数
- leetcode:Remove Duplicates from Sorted Array II (允许重复一次,去掉数组多余数字)【面试算法题】
- leetcode 381. Insert Delete GetRandom O(1) - Duplicates allowed