|
| 1 | +package medium; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.Map; |
| 5 | +import java.util.Random; |
| 6 | + |
| 7 | +/**381. Insert Delete GetRandom O(1) - Duplicates allowed QuestionEditorial Solution My Submissions |
| 8 | +Total Accepted: 190 |
| 9 | +Total Submissions: 613 |
| 10 | +Difficulty: Medium |
| 11 | +Design a data structure that supports all following operations in average O(1) time. |
| 12 | +
|
| 13 | +Note: Duplicate elements are allowed. |
| 14 | +insert(val): Inserts an item val to the collection. |
| 15 | +remove(val): Removes an item val from the collection if present. |
| 16 | +getRandom: Returns a random element from current collection of elements. The probability of each element being returned is linearly related to the number of same value the collection contains. |
| 17 | +Example: |
| 18 | +
|
| 19 | +// Init an empty collection. |
| 20 | +RandomizedCollection collection = new RandomizedCollection(); |
| 21 | +
|
| 22 | +// Inserts 1 to the collection. Returns true as the collection did not contain 1. |
| 23 | +collection.insert(1); |
| 24 | +
|
| 25 | +// Inserts another 1 to the collection. Returns false as the collection contained 1. Collection now contains [1,1]. |
| 26 | +collection.insert(1); |
| 27 | +
|
| 28 | +// Inserts 2 to the collection, returns true. Collection now contains [1,1,2]. |
| 29 | +collection.insert(2); |
| 30 | +
|
| 31 | +// getRandom should return 1 with the probability 2/3, and returns 2 with the probability 1/3. |
| 32 | +collection.getRandom(); |
| 33 | +
|
| 34 | +// Removes 1 from the collection, returns true. Collection now contains [1,2]. |
| 35 | +collection.remove(1); |
| 36 | +
|
| 37 | +// getRandom should return 1 and 2 both equally likely. |
| 38 | +collection.getRandom();*/ |
| 39 | +public class RandomizedCollection { |
| 40 | + |
| 41 | + Map<Integer, Integer> forwardMap;//key is the to-be-inserted number, value is its auto-incremented index |
| 42 | + Map<Integer, Integer> reverseMap;//the other way around |
| 43 | + int index; |
| 44 | + Random rand; |
| 45 | + |
| 46 | + /** Initialize your data structure here. */ |
| 47 | + public RandomizedCollection() { |
| 48 | + forwardMap = new HashMap(); |
| 49 | + reverseMap = new HashMap(); |
| 50 | + index = 0; |
| 51 | + rand = new Random(); |
| 52 | + } |
| 53 | + |
| 54 | + /** Inserts a value to the collection. Returns true if the collection did not already contain the specified element. */ |
| 55 | + public boolean insert(int val) { |
| 56 | + boolean contains; |
| 57 | + if (reverseMap.containsValue(val)) { |
| 58 | + contains = true; |
| 59 | + } else { |
| 60 | + contains = false; |
| 61 | + } |
| 62 | + forwardMap.put(val, index);//this will overwrite the preivous key with a new index if the key already exists |
| 63 | + reverseMap.put(index, val); |
| 64 | + index++; |
| 65 | + return contains; |
| 66 | + } |
| 67 | + |
| 68 | + /** Removes a value from the collection. Returns true if the collection contained the specified element. */ |
| 69 | + public boolean remove(int val) { |
| 70 | + boolean contains; |
| 71 | + if (reverseMap.containsValue(val)) { |
| 72 | + contains = true; |
| 73 | + if(forwardMap.containsKey(val)) { |
| 74 | + int i = forwardMap.get(val); |
| 75 | + forwardMap.remove(val); |
| 76 | + reverseMap.remove(i); |
| 77 | + } else { |
| 78 | + //remove the entry in revserve map that has val as its value |
| 79 | + reverseMap.values().remove(val); |
| 80 | + } |
| 81 | + } else { |
| 82 | + contains = false; |
| 83 | + } |
| 84 | + return contains; |
| 85 | + } |
| 86 | + |
| 87 | + /** Get a random element from the collection. */ |
| 88 | + public int getRandom() { |
| 89 | + int randNum = rand.nextInt(index); |
| 90 | + while(!reverseMap.containsKey(randNum)){ |
| 91 | + randNum = rand.nextInt(index); |
| 92 | + } |
| 93 | + return reverseMap.get(randNum); |
| 94 | + } |
| 95 | + |
| 96 | + public static void main(String...strings){ |
| 97 | + RandomizedCollection test = new RandomizedCollection(); |
| 98 | + System.out.println(test.insert(1)); |
| 99 | + System.out.println(test.insert(1)); |
| 100 | + System.out.println(test.insert(2)); |
| 101 | + System.out.println(test.getRandom()); |
| 102 | + System.out.println(test.remove(1)); |
| 103 | + System.out.println(test.getRandom()); |
| 104 | + } |
| 105 | + |
| 106 | +} |
0 commit comments