|
| 1 | +package by.andd3dfx.collections; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Map; |
| 7 | +import java.util.Random; |
| 8 | + |
| 9 | +/** |
| 10 | + * <pre> |
| 11 | + * <a href="https://leetcode.com/problems/insert-delete-getrandom-o1/description/">Task description</a> |
| 12 | + * |
| 13 | + * Implement the RandomizedSet class: |
| 14 | + * |
| 15 | + * RandomizedSet() Initializes the RandomizedSet object. |
| 16 | + * bool insert(int val) Inserts an item val into the set if not present. Returns true if the item was not present, false otherwise. |
| 17 | + * bool remove(int val) Removes an item val from the set if present. Returns true if the item was present, false otherwise. |
| 18 | + * 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. |
| 19 | + * |
| 20 | + * You must implement the functions of the class such that each function works in average O(1) time complexity. |
| 21 | + * |
| 22 | + * Example 1: |
| 23 | + * Input |
| 24 | + * ["RandomizedSet", "insert", "remove", "insert", "getRandom", "remove", "insert", "getRandom"] |
| 25 | + * [[], [1], [2], [2], [], [1], [2], []] |
| 26 | + * Output |
| 27 | + * [null, true, false, true, 2, true, false, 2] |
| 28 | + * |
| 29 | + * Explanation |
| 30 | + * RandomizedSet randomizedSet = new RandomizedSet(); |
| 31 | + * randomizedSet.insert(1); // Inserts 1 to the set. Returns true as 1 was inserted successfully. |
| 32 | + * randomizedSet.remove(2); // Returns false as 2 does not exist in the set. |
| 33 | + * randomizedSet.insert(2); // Inserts 2 to the set, returns true. Set now contains [1,2]. |
| 34 | + * randomizedSet.getRandom(); // getRandom() should return either 1 or 2 randomly. |
| 35 | + * randomizedSet.remove(1); // Removes 1 from the set, returns true. Set now contains [2]. |
| 36 | + * randomizedSet.insert(2); // 2 was already in the set, so return false. |
| 37 | + * randomizedSet.getRandom(); // Since 2 is the only number in the set, getRandom() will always return 2. |
| 38 | + * </pre> |
| 39 | + */ |
| 40 | +public class RandomizedSet { |
| 41 | + |
| 42 | + private final Random random = new Random(); |
| 43 | + private final Map<Integer, Integer> map = new HashMap<>(); |
| 44 | + private final List<Integer> keys = new ArrayList<>(); |
| 45 | + |
| 46 | + /** |
| 47 | + * Inserts an item val into the set if not present. Returns true if the item was not present, false otherwise. |
| 48 | + */ |
| 49 | + public boolean insert(int val) { |
| 50 | + var isNotExist = !map.containsKey(val); |
| 51 | + if (isNotExist) { |
| 52 | + keys.add(val); |
| 53 | + map.put(val, keys.size() - 1); |
| 54 | + } |
| 55 | + return isNotExist; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Removes an item val from the set if present. Returns true if the item was present, false otherwise. |
| 60 | + */ |
| 61 | + public boolean remove(int val) { |
| 62 | + var isExist = map.containsKey(val); |
| 63 | + if (isExist) { |
| 64 | + var index = map.get(val); |
| 65 | + keys.set(index, keys.getLast()); |
| 66 | + map.put(keys.get(index), index); |
| 67 | + keys.removeLast(); |
| 68 | + map.remove(val); |
| 69 | + } |
| 70 | + return isExist; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Returns a random element from the current set of elements (it's guaranteed that at least one element exists |
| 75 | + * when this method is called). Each element must have the same probability of being returned. |
| 76 | + */ |
| 77 | + public int getRandom() { |
| 78 | + var index = random.nextInt(keys.size()); |
| 79 | + return keys.get(index); |
| 80 | + } |
| 81 | +} |
0 commit comments