structItem{intval;intindexInMap;Item(intval,intindexInMap):val(val),indexInMap(indexInMap){}};classRandomizedCollection{public:/** Inserts a value to the collection. Returns true if the collection did not * already contain the specified element. */boolinsert(intval){valToIndices[val].push_back(items.size());items.emplace_back(val,valToIndices[val].size()-1);returnvalToIndices[val].size()==1;}/** Removes a value from the collection. Returns true if the collection * contained the specified element. */boolremove(intval){if(!valToIndices.count(val))returnfalse;constintindex=valToIndices[val].back();valToIndices[items.back().val][items.back().indexInMap]=index;valToIndices[val].pop_back();if(valToIndices[val].empty())valToIndices.erase(val);items[index]=items.back();items.pop_back();returntrue;}/** Get a random element from the collection. */intgetRandom(){constintindex=rand()%items.size();returnitems[index].val;}private:unordered_map<int,vector<int>>valToIndices;vector<Item>items;};
classItem{publicintval;publicintindexInMap;publicItem(intval,intindexInMap){this.val=val;this.indexInMap=indexInMap;}}classRandomizedCollection{/** * Inserts a value to the collection. Returns true if the collection did not already contain the * specified element. */publicbooleaninsert(intval){valToIndices.putIfAbsent(val,newArrayList<>());valToIndices.get(val).add(items.size());items.add(newItem(val,valToIndices.get(val).size()-1));returnvalToIndices.get(val).size()==1;}/** * Removes a value from the collection. Returns true if the collection contained the specified * element. */publicbooleanremove(intval){if(!valToIndices.containsKey(val))returnfalse;finalintindex=lastIndex(valToIndices.get(val));valToIndices.get(last(items).val).set(last(items).indexInMap,index);finalintindicesSize=valToIndices.get(val).size();valToIndices.get(val).remove(indicesSize-1);if(valToIndices.get(val).isEmpty())valToIndices.remove(val);items.set(index,last(items));items.remove(items.size()-1);returntrue;}/** Get a random element from the collection. */publicintgetRandom(){finalintindex=rand.nextInt(items.size());returnitems.get(index).val;}privateMap<Integer,List<Integer>>valToIndices=newHashMap<>();privateList<Item>items=newArrayList<>();privateRandomrand=newRandom();privateintlastIndex(List<Integer>indices){returnindices.get(indices.size()-1);}privateItemlast(List<Item>items){returnitems.get(items.size()-1);}}
classRandomizedCollection:def__init__(self):""" Initialize your data structure here. """self.vals=[]self.valToIndices=defaultdict(list)definsert(self,val:int)->bool:""" Inserts a value to the collection. Returns true if the collection did not already contain the specified element. """self.valToIndices[val].append(len(self.vals))self.vals.append([val,len(self.valToIndices[val])-1])returnlen(self.valToIndices[val])==1defremove(self,val:int)->bool:""" Removes a value from the collection. Returns true if the collection contained the specified element. """ifvalnotinself.valToIndicesorself.valToIndices[val]==[]:returnFalseindex=self.valToIndices[val][-1]self.valToIndices[self.vals[-1][0]][self.vals[-1][1]]=indexself.valToIndices[val].pop()self.vals[index]=self.vals[-1]self.vals.pop()returnTruedefgetRandom(self)->int:""" Get a random element from the collection. """index=randint(0,len(self.vals)-1)returnself.vals[index][0]