|
| 1 | +// Source : https://leetcode.com/problems/insert-delete-getrandom-o1-duplicates-allowed/ |
| 2 | +// Author : Hao Chen |
| 3 | +// Date : 2016-08-25 |
| 4 | + |
| 5 | +/*************************************************************************************** |
| 6 | + * |
| 7 | + * Design a data structure that supports all following operations in average O(1) time. |
| 8 | + * Note: Duplicate elements are allowed. |
| 9 | + * |
| 10 | + * insert(val): Inserts an item val to the collection. |
| 11 | + * remove(val): Removes an item val from the collection if present. |
| 12 | + * getRandom: Returns a random element from current collection of elements. The |
| 13 | + * probability of each element being returned is linearly related to the number of same |
| 14 | + * value the collection contains. |
| 15 | + * |
| 16 | + * Example: |
| 17 | + * |
| 18 | + * // Init an empty collection. |
| 19 | + * RandomizedCollection collection = new RandomizedCollection(); |
| 20 | + * |
| 21 | + * // Inserts 1 to the collection. Returns true as the collection did not contain 1. |
| 22 | + * collection.insert(1); |
| 23 | + * |
| 24 | + * // Inserts another 1 to the collection. Returns false as the collection contained 1. |
| 25 | + * Collection now contains [1,1]. |
| 26 | + * collection.insert(1); |
| 27 | + * |
| 28 | + * // Inserts 2 to the collection, returns true. Collection now contains [1,1,2]. |
| 29 | + * collection.insert(2); |
| 30 | + * |
| 31 | + * // getRandom should return 1 with the probability 2/3, and returns 2 with the |
| 32 | + * probability 1/3. |
| 33 | + * collection.getRandom(); |
| 34 | + * |
| 35 | + * // Removes 1 from the collection, returns true. Collection now contains [1,2]. |
| 36 | + * collection.remove(1); |
| 37 | + * |
| 38 | + * // getRandom should return 1 and 2 both equally likely. |
| 39 | + * collection.getRandom(); |
| 40 | + ***************************************************************************************/ |
| 41 | + |
| 42 | +class RandomizedCollection { |
| 43 | +public: |
| 44 | + /** Initialize your data structure here. */ |
| 45 | + RandomizedCollection() { |
| 46 | + srand(time(NULL)); |
| 47 | + } |
| 48 | + |
| 49 | + /** Inserts a value to the collection. Returns true if the collection did not already contain the specified element. */ |
| 50 | + bool insert(int val) { |
| 51 | + data.push_back(val); |
| 52 | + valpos[val].insert( data.size() - 1 ); |
| 53 | + return (valpos[val].size() == 1); |
| 54 | + } |
| 55 | + |
| 56 | + /** Removes a value from the collection. Returns true if the collection contained the specified element. */ |
| 57 | + bool remove(int val) { |
| 58 | + // not found |
| 59 | + if (!find(val)) return false; |
| 60 | + |
| 61 | + |
| 62 | + //same idea with non-duplication version, but need be careful with some edge case |
| 63 | + int _idx = *(valpos[val].begin()); |
| 64 | + int _val = data.back(); |
| 65 | + |
| 66 | + valpos[_val].insert(_idx); |
| 67 | + data[_idx] = _val; |
| 68 | + |
| 69 | + valpos[val].erase(_idx); |
| 70 | + if (valpos[val].size()==0){ |
| 71 | + valpos.erase(val); |
| 72 | + } |
| 73 | + |
| 74 | + data.pop_back(); |
| 75 | + if ( _idx < data.size() ){ |
| 76 | + valpos[_val].erase(data.size()); |
| 77 | + valpos[_val].insert(_idx); |
| 78 | + } |
| 79 | + |
| 80 | + return true; |
| 81 | + } |
| 82 | + |
| 83 | + /** Get a random element from the collection. */ |
| 84 | + int getRandom() { |
| 85 | + return data[ rand() % data.size() ]; |
| 86 | + } |
| 87 | +private: |
| 88 | + unordered_map<int, unordered_set<int>> valpos; //value position map |
| 89 | + vector<int> data; |
| 90 | + bool find(int val) { |
| 91 | + return (valpos.find(val) != valpos.end()); |
| 92 | + } |
| 93 | + |
| 94 | +}; |
| 95 | + |
| 96 | +/** |
| 97 | + * Your RandomizedCollection object will be instantiated and called as such: |
| 98 | + * RandomizedCollection obj = new RandomizedCollection(); |
| 99 | + * bool param_1 = obj.insert(val); |
| 100 | + * bool param_2 = obj.remove(val); |
| 101 | + * int param_3 = obj.getRandom(); |
| 102 | + */ |
0 commit comments