Insert Delete Get Random O(1)
Implement the RandomizedSet
-
Initializes theRandomizedSet() object.RandomizedSet -
Inserts an itembool insert(int val) into the set if not present. Returnsval if the item was not present,true otherwise.false -
Removes an itembool remove(int val) from the set if present. Returnsval if the item was present,true otherwise.false -
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.int getRandom()
You must implement the functions of the class such that each function works in average O(1)
Example 1
[[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
calls will be made to2 * 105 ,insert , andremove .getRandom
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 HashMapArrayListHashMapinsert()remove()O(1)getRandom()O(1)ArrayListHashMap
Implementation details:
-
Initialize a
that we are going to store elements into, call itArrayList , anumbers that we are going to use it to store elements and key and index as value, call itHashMap .numberToIndex -
insert() operation: If the
already contains the input element, returnnumberToIndex , otherwise add it to thefalse as well as tonumbers and returnnumberToIndex .true -
remove() operation: If the
doesn't contains the input element, returnnumberToIndex , otherwise check the index of the element fromfalse and if this is the last element innumberToIndex , then remove it fromnumbers andnumbers . If the index of the element is not the end index, then we will replace that element with end element innumberToIndex , and update end element indexed element to current index innumbers . Then, remove end element fromnumberToIndex and remove input element fromnumbers .numberToIndex
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 the to reflect it.numberToIndex -
getRandom() operation: To get random number within the range of
indexes range, we will usenumbers class'sjava.util.Random method, and return that random indexed element fromnextInt(int bound) .numbers
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
public class InsertDeleteGetRandom {
final HashMap<Integer, Integer> numberToIndex;
final List<Integer> numbers;
final Random randomGen;
public InsertDeleteGetRandom() {
numberToIndex = new HashMap<>();
numbers = new ArrayList<>();
randomGen = new Random();
}
public boolean insert(int val) {
if(numberToIndex.containsKey(val)) {
return false;
}
numberToIndex.put(val, numbers.size());
numbers.add(val);
return true;
}
public boolean remove(int val) {
if(!numberToIndex.containsKey(val)) {
return false;
}
int index = numberToIndex.get(val);
int lastIndex = numbers.size()-1;
if(index != lastIndex) { // re-arrange numbers
// update the index for last element of list in the map
// replace the element that we are going tot delete with the last element in the List
int lastElement = numbers.get(lastIndex);
numbers.set(index, lastElement);
numberToIndex.put(lastElement, index);
}
numbers.remove(lastIndex);
numberToIndex.remove(val);
return true;
}
public int getRandom() {
int random = randomGen.nextInt(numbers.size());
return numbers.get(random);
}
public static void main(String[] args) {
InsertDeleteGetRandom insertDeleteGetRandom = new InsertDeleteGetRandom();
System.out.println(insertDeleteGetRandom.insert(1));
System.out.println(insertDeleteGetRandom.remove(2));
System.out.println(insertDeleteGetRandom.insert(2));
System.out.println(insertDeleteGetRandom.getRandom());
System.out.println(insertDeleteGetRandom.remove(1));
System.out.println(insertDeleteGetRandom.insert(2));
System.out.println(insertDeleteGetRandom.getRandom());
insertDeleteGetRandom = new InsertDeleteGetRandom();
System.out.println(insertDeleteGetRandom.remove(0));
System.out.println(insertDeleteGetRandom.remove(0));
System.out.println(insertDeleteGetRandom.insert(0));
System.out.println(insertDeleteGetRandom.getRandom());
System.out.println(insertDeleteGetRandom.remove(0));
System.out.println(insertDeleteGetRandom.insert(0));
System.out.println(insertDeleteGetRandom.getRandom());
}
}
Complexity Analysis:
Time complexity: Above code runs in O(1) time for all operations insert()remove()getRandom()
Space complexity: O(n), since we are storing elements into a HashMapArrayList