1
+ package com .fishercoder .solutions ;
2
+
3
+ import java .util .HashMap ;
4
+ import java .util .Map ;
5
+ import java .util .Random ;
6
+
7
+ /**
8
+ * 380. Insert Delete GetRandom O(1)
9
+ * Design a data structure that supports all following operations in average O(1) time.
10
+ * <p>
11
+ * insert(val): Inserts an item val to the set if not already present.
12
+ * remove(val): Removes an item val from the set if present.
13
+ * getRandom: Returns a random element from current set of elements. Each element must have the same probability of being returned.
14
+ * Example:
15
+ * <p>
16
+ * // Init an empty set.
17
+ * RandomizedSet randomSet = new RandomizedSet();
18
+ * <p>
19
+ * // Inserts 1 to the set. Returns true as 1 was inserted successfully.
20
+ * randomSet.insert(1);
21
+ * <p>
22
+ * // Returns false as 2 does not exist in the set.
23
+ * randomSet.remove(2);
24
+ * <p>
25
+ * // Inserts 2 to the set, returns true. Set now contains [1,2].
26
+ * randomSet.insert(2);
27
+ * <p>
28
+ * // getRandom should return either 1 or 2 randomly.
29
+ * randomSet.getRandom();
30
+ * <p>
31
+ * // Removes 1 from the set, returns true. Set now contains [2].
32
+ * randomSet.remove(1);
33
+ * <p>
34
+ * // 2 was already in the set, so return false.
35
+ * randomSet.insert(2);
36
+ * <p>
37
+ * // Since 2 is the only number in the set, getRandom always return 2.
38
+ * randomSet.getRandom();
39
+ */
40
+
41
+ public class _380 {
42
+ /**
43
+ * Your _380 object will be instantiated and called as such:
44
+ * _380 obj = new _380();
45
+ * boolean param_1 = obj.insert(val);
46
+ * boolean param_2 = obj.delete(val);
47
+ * int param_3 = obj.getRandom();
48
+ */
49
+
50
+ public static class RandomizedSet {
51
+
52
+ Map <Integer , Integer > forwardMap ;//key is auto increment index, value if the inserted val
53
+ Map <Integer , Integer > reverseMap ;//the other way around
54
+ int index ;
55
+ Random random ;
56
+
57
+ /**
58
+ * Initialize your data structure here.
59
+ */
60
+ public RandomizedSet () {
61
+ forwardMap = new HashMap ();
62
+ reverseMap = new HashMap ();
63
+ index = 0 ;
64
+ random = new Random ();
65
+ }
66
+
67
+ /**
68
+ * Inserts a value to the set. Returns true if the set did not already contain the specified element.
69
+ */
70
+ public boolean insert (int val ) {
71
+ if (forwardMap .containsValue (val )) return false ;
72
+ else {
73
+ forwardMap .put (index , val );
74
+ reverseMap .put (val , index ++);
75
+ return true ;
76
+ }
77
+ }
78
+
79
+ /**
80
+ * Deletes a value from the set. Returns true if the set contained the specified element.
81
+ */
82
+ public boolean remove (int val ) {
83
+ if (forwardMap .containsValue (val )) {
84
+ int key = reverseMap .get (val );
85
+ reverseMap .remove (val );
86
+ forwardMap .remove (key );
87
+ return true ;
88
+ } else {
89
+ return false ;
90
+ }
91
+ }
92
+
93
+ /**
94
+ * Get a random element from the set.
95
+ */
96
+ public int getRandom () {
97
+ int max = forwardMap .size ();
98
+ if (max == 1 ) return forwardMap .get (index - 1 );
99
+ int randomNum = random .nextInt (max );
100
+ while (!forwardMap .containsKey (randomNum )) {
101
+ randomNum = random .nextInt (max );
102
+ }
103
+ return forwardMap .get (randomNum );
104
+ }
105
+ }
106
+ }
0 commit comments