Implement the
-
RandomizedSet() Initializes theRandomizedSet object. -
bool insert(int val) Inserts an itemval into the set if not present. Returnstrue if the item was not present,false otherwise. -
bool remove(int val) Removes an itemval from the set if present. Returnstrue if the item was present,false otherwise. -
int getRandom() Returns a random element from the current set of elements (it's guaranteed that at least one element exists when this method is called). Each element must have the same probability of being returned.
You must implement the functions of the class such that each function works in average
[[1], [2], [2], [], [1], [2], []]
Output: [true, false, true, 2, true, false, 2]
Explanation:
randomizedSet.insert(1); // Inserts 1 to the set. Returns true as 1 was inserted successfully.
randomizedSet.remove(2); // Returns false as 2 does not exist in the set.
randomizedSet.insert(2); // Inserts 2 to the set, returns true. Set now contains [1,2].
randomizedSet.getRandom(); // getRandom() should return either 1 or 2 randomly.
randomizedSet.remove(1); // Removes 1 from the set, returns true. Set now contains [2].
randomizedSet.insert(2); // 2 was already in the set, so return false.
randomizedSet.getRandom(); // Since 2 is the only number in the set, getRandom() will always return 2.
Constraints:
-231 <= val <= 231 - 1 - At most
2 * 105 calls will be made toinsert ,remove , andgetRandom .
Contents
Solution 1 - Using a HashMap to have Insert() and Remove() operations in O(1) time, and a List to track current elements and to have Get() operation in O(1) time
In this approach, we are going to use a
Implementation details:
-
Initialize a
ArrayList that we are going to store elements into, call itnumbers , aHashMap that we are going to use it to store elements and key and index as value, call itnumberToIndex . -
insert() operation: If the
numberToIndex already contains the input element, returnfalse , otherwise add it to thenumbers as well as tonumberToIndex and returntrue . -
remove() operation: If the
numberToIndex doesn't contains the input element, returnfalse , otherwise check the index of the element fromnumberToIndex and if this is the last element innumbers , then remove it fromnumbers andnumberToIndex . If the index of the element is not the end index, then we will replace that element with end element innumbers , and update end element indexed element to current index innumberToIndex . Then, remove end element fromnumbers and remove input element fromnumberToIndex .
The reason we do this is because, if the element is not at the end, then removing an element will require resizing of the array and this is O(n) operation, so instead or removing the element, we replace that with end indexed element and remove that end element, and update the end element index in thenumberToIndex to reflect it. -
getRandom() operation: To get random number within the range of
numbers indexes range, we will usejava.util.Random class'snextInt(int bound) method, and return that random indexed element fromnumbers .
Complexity Analysis:
Time complexity: Above code runs in O(1) time for all operations
Space complexity: O(n), since we are storing elements into a
Above implementations source code can be found at