Insert Delete GetRandom O(1) - Duplicates allowed Solutions in C++
Number 381
Difficulty Hard
Acceptance 34.1%
Link LeetCode
Other languages —
Solutions
C++ solution by haoel/leetcode
// Source : https://leetcode.com/problems/insert-delete-getrandom-o1-duplicates-allowed/// Author : Hao Chen// Date : 2016-08-25class RandomizedCollection {public:/** Initialize your data structure here. */RandomizedCollection() {srand(time(NULL));}/** Inserts a value to the collection. Returns true if the collection did not already contain the specified element. */bool insert(int val) {data.push_back(val);valpos[val].insert( data.size() - 1 );return (valpos[val].size() == 1);}/** Removes a value from the collection. Returns true if the collection contained the specified element. */bool remove(int val) {// not foundif (!find(val)) return false;//same idea with non-duplication version, but need be careful with some edge caseint _idx = *(valpos[val].begin());int _val = data.back();valpos[_val].insert(_idx);data[_idx] = _val;valpos[val].erase(_idx);if (valpos[val].size()==0){valpos.erase(val);}data.pop_back();if ( _idx < data.size() ){valpos[_val].erase(data.size());valpos[_val].insert(_idx);}return true;}/** Get a random element from the collection. */int getRandom() {return data[ rand() % data.size() ];}private:unordered_map<int, unordered_set<int>> valpos; //value position mapvector<int> data;bool find(int val) {return (valpos.find(val) != valpos.end());}};/*** 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();*/
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/insert-delete-getrandom-o1-duplicates-allowed/description//// Author : liuyubobobo/// Time : 2018-07-08#include <iostream>#include <vector>#include <unordered_map>#include <unordered_set>using namespace std;/// Vector to stroe all elements/// HashMap to restore all maps between elements and index/// Time Complexity: O(1)/// Space Complexity: O(n)class RandomizedCollection {private:vector<int> nums;unordered_map<int, unordered_set<int>> indexes;public:/** Initialize your data structure here. */RandomizedCollection() {nums.clear();indexes.clear();}/** Inserts a value to the collection. Returns true if the collection did not already contain the specified element. */bool insert(int val) {nums.push_back(val);indexes[val].insert(nums.size() - 1);return indexes[val].size() == 1;}/** Removes a value from the collection. Returns true if the collection contained the specified element. */bool remove(int val) {if(indexes.find(val) != indexes.end()){int i = *indexes[val].begin();indexes[val].erase(i);if(indexes[val].size() == 0)indexes.erase(val);int num = nums.back();nums.pop_back();if(!(num == val && nums.size() == i)){nums[i] = num;indexes[num].erase(nums.size());indexes[num].insert(i);}return true;}return false;}/** Get a random element from the collection. */int getRandom() {int rndIndex = rand() % nums.size();return nums[rndIndex];}};int main() {return 0;}