|
| 1 | +packagemedium; |
| 2 | + |
| 3 | +importjava.util.HashMap; |
| 4 | +importjava.util.Map; |
| 5 | +importjava.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 | +publicclassRandomizedCollection { |
| 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 | +intindex; |
| 44 | +Randomrand; |
| 45 | + |
| 46 | +/** Initialize your data structure here. */ |
| 47 | +publicRandomizedCollection() { |
| 48 | +forwardMap =newHashMap(); |
| 49 | +reverseMap =newHashMap(); |
| 50 | +index =0; |
| 51 | +rand =newRandom(); |
| 52 | + } |
| 53 | + |
| 54 | +/** Inserts a value to the collection. Returns true if the collection did not already contain the specified element. */ |
| 55 | +publicbooleaninsert(intval) { |
| 56 | +booleancontains; |
| 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 | +returncontains; |
| 66 | + } |
| 67 | + |
| 68 | +/** Removes a value from the collection. Returns true if the collection contained the specified element. */ |
| 69 | +publicbooleanremove(intval) { |
| 70 | +booleancontains; |
| 71 | +if (reverseMap.containsValue(val)) { |
| 72 | +contains =true; |
| 73 | +if(forwardMap.containsKey(val)) { |
| 74 | +inti =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 | +returncontains; |
| 85 | + } |
| 86 | + |
| 87 | +/** Get a random element from the collection. */ |
| 88 | +publicintgetRandom() { |
| 89 | +intrandNum =rand.nextInt(index); |
| 90 | +while(!reverseMap.containsKey(randNum)){ |
| 91 | +randNum =rand.nextInt(index); |
| 92 | + } |
| 93 | +returnreverseMap.get(randNum); |
| 94 | + } |
| 95 | + |
| 96 | +publicstaticvoidmain(String...strings){ |
| 97 | +RandomizedCollectiontest =newRandomizedCollection(); |
| 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 | +} |