|
| 1 | +/* |
| 2 | + * @lc app=leetcode id=380 lang=java |
| 3 | + * |
| 4 | + * [380] Insert Delete GetRandom O(1) |
| 5 | + * |
| 6 | + * https://leetcode.com/problems/insert-delete-getrandom-o1/description/ |
| 7 | + * |
| 8 | + * algorithms |
| 9 | + * Medium (41.99%) |
| 10 | + * Likes: 1152 |
| 11 | + * Dislikes: 90 |
| 12 | + * Total Accepted: 117.7K |
| 13 | + * Total Submissions: 273.9K |
| 14 | + * Testcase Example: '["RandomizedSet","insert","remove","insert","getRandom","remove","insert","getRandom"]\n[[],[1],[2],[2],[],[1],[2],[]]' |
| 15 | + * |
| 16 | + * Design a data structure that supports all following operations in average |
| 17 | + * O(1) time. |
| 18 | + * |
| 19 | + * |
| 20 | + * |
| 21 | + * insert(val): Inserts an item val to the set if not already present. |
| 22 | + * remove(val): Removes an item val from the set if present. |
| 23 | + * getRandom: Returns a random element from current set of elements. Each |
| 24 | + * element must have the same probability of being returned. |
| 25 | + * |
| 26 | + * |
| 27 | + * |
| 28 | + * Example: |
| 29 | + * |
| 30 | + * // Init an empty set. |
| 31 | + * RandomizedSet randomSet = new RandomizedSet(); |
| 32 | + * |
| 33 | + * // Inserts 1 to the set. Returns true as 1 was inserted successfully. |
| 34 | + * randomSet.insert(1); |
| 35 | + * |
| 36 | + * // Returns false as 2 does not exist in the set. |
| 37 | + * randomSet.remove(2); |
| 38 | + * |
| 39 | + * // Inserts 2 to the set, returns true. Set now contains [1,2]. |
| 40 | + * randomSet.insert(2); |
| 41 | + * |
| 42 | + * // getRandom should return either 1 or 2 randomly. |
| 43 | + * randomSet.getRandom(); |
| 44 | + * |
| 45 | + * // Removes 1 from the set, returns true. Set now contains [2]. |
| 46 | + * randomSet.remove(1); |
| 47 | + * |
| 48 | + * // 2 was already in the set, so return false. |
| 49 | + * randomSet.insert(2); |
| 50 | + * |
| 51 | + * // Since 2 is the only number in the set, getRandom always return 2. |
| 52 | + * randomSet.getRandom(); |
| 53 | + * |
| 54 | + * |
| 55 | + */ |
| 56 | +class RandomizedSet { |
| 57 | + |
| 58 | + List<Integer> list; |
| 59 | + Map<Integer, Integer> map; |
| 60 | + Random random; |
| 61 | + |
| 62 | + /** Initialize your data structure here. */ |
| 63 | + public RandomizedSet() { |
| 64 | + list = new ArrayList<>(); |
| 65 | + map = new HashMap<>(); |
| 66 | + random = new Random(); |
| 67 | + } |
| 68 | + |
| 69 | + /** Inserts a value to the set. Returns true if the set did not already contain the specified element. */ |
| 70 | + public boolean insert(int val) { |
| 71 | + if (map.containsKey(val)) { |
| 72 | + return false; |
| 73 | + } |
| 74 | + map.put(val, list.size()); |
| 75 | + list.add(val); |
| 76 | + return true; |
| 77 | + } |
| 78 | + |
| 79 | + /** Removes a value from the set. Returns true if the set contained the specified element. */ |
| 80 | + public boolean remove(int val) { |
| 81 | + if (!map.containsKey(val)) { |
| 82 | + return false; |
| 83 | + } |
| 84 | + int index = map.get(val); |
| 85 | + if (index < list.size() - 1) { |
| 86 | + int last = list.get(list.size() - 1); |
| 87 | + map.put(last, index); |
| 88 | + list.set(index, last); |
| 89 | + } |
| 90 | + map.remove(val); |
| 91 | + list.remove(list.size() - 1); |
| 92 | + return true; |
| 93 | + } |
| 94 | + |
| 95 | + /** Get a random element from the set. */ |
| 96 | + public int getRandom() { |
| 97 | + int index = random.nextInt(list.size()); |
| 98 | + return list.get(index); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +/** |
| 103 | + * Your RandomizedSet object will be instantiated and called as such: |
| 104 | + * RandomizedSet obj = new RandomizedSet(); |
| 105 | + * boolean param_1 = obj.insert(val); |
| 106 | + * boolean param_2 = obj.remove(val); |
| 107 | + * int param_3 = obj.getRandom(); |
| 108 | + */ |
| 109 | + |
0 commit comments