Related:
LeetCode 380 - Insert Delete GetRandom O(1)
LeetCode 381 - Insert Delete GetRandom O(1) - Duplicates allowed
https://www.hrwhisper.me/leetcode-insert-delete-getrandom-o1-duplicates-allowed/
https://zxi.mytechroad.com/blog/hashtable/leetcode-381-insert-delete-getrandom-o1-duplicates-allowed/
https://leetcode.com/problems/insert-delete-getrandom-o1-duplicates-allowed/discuss/85540/Java-HaspMap-LinkedHashSet-ArrayList-(155-ms)
https://github.com/mintycc/OnlineJudge-Solutions/blob/master/Leetcode/381_Insert_Delete_GetRandom_O(1)_Duplicates_Allowed.java
LeetCode 380 - Insert Delete GetRandom O(1)
LeetCode 381 - Insert Delete GetRandom O(1) - Duplicates allowed
https://www.hrwhisper.me/leetcode-insert-delete-getrandom-o1-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();
题目大意: 要求实现一个数据结构(数允许重复),可以支持插入,删除,随机数生成。 他们的复杂度均要求O(1)
思路:其实和上一题 leetcode Insert Delete GetRandom O(1) 差不多,只不过这题允许重复,把字典里换成一个list即可
private:
unordered_map<int, vector<int>> index;
vector<int> output;
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) {
bool return_val = index.find(val) == index.end();
index[val].push_back(output.size());
output.push_back(val);
return return_val;
}
/** Removes a value from the collection. Returns true if the collection contained the specified element. */
bool remove(int val) {
if (index.find(val) == index.end()) return false;
int _id = index[val].back(); index[val].pop_back();
int last = output.back(); output.pop_back();
if (last != val) {
index[last].back() = _id;
output[_id] = last;
}
if (index[val].empty())
index.erase(val);
return true;
}
/** Get a random element from the collection. */
int getRandom() {
return output[rand() % output.size()];
}
https://leetcode.com/problems/insert-delete-getrandom-o1-duplicates-allowed/discuss/85540/Java-HaspMap-LinkedHashSet-ArrayList-(155-ms)
public class RandomizedCollection {
ArrayList<Integer> nums;
HashMap<Integer, Set<Integer>> locs;
java.util.Random rand = new java.util.Random();
/** Initialize your data structure here. */
public RandomizedCollection() {
nums = new ArrayList<Integer>();
locs = new HashMap<Integer, Set<Integer>>();
}
/** 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 = locs.containsKey(val);
if ( ! contain ) locs.put( val, new LinkedHashSet<Integer>() );
locs.get(val).add(nums.size());
nums.add(val);
return ! contain ;
}
/** Removes a value from the collection. Returns true if the collection contained the specified element. */
public boolean remove(int val) {
boolean contain = locs.containsKey(val);
if ( ! contain ) return false;
int loc = locs.get(val).iterator().next();
locs.get(val).remove(loc);
if (loc < nums.size() - 1 ) {
int lastone = nums.get( nums.size()-1 );
nums.set( loc , lastone );
locs.get(lastone).remove( nums.size()-1);
locs.get(lastone).add(loc);
}
nums.remove(nums.size() - 1);
if (locs.get(val).isEmpty()) locs.remove(val);
return true;
}
/** Get a random element from the collection. */
public int getRandom() {
return nums.get( rand.nextInt(nums.size()) );
}
}
https://github.com/mintycc/OnlineJudge-Solutions/blob/master/Leetcode/381_Insert_Delete_GetRandom_O(1)_Duplicates_Allowed.java