|
| 1 | +""" |
| 2 | +Design a data structure that supports all following operations in average O(1) time. |
| 3 | +
|
| 4 | +insert(val): Inserts an item val to the set if not already present. |
| 5 | +remove(val): Removes an item val from the set if present. |
| 6 | +getRandom: Returns a random element from current set of elements. Each element must have the same probability of |
| 7 | +being returned. |
| 8 | +Example: |
| 9 | +
|
| 10 | +// Init an empty set. |
| 11 | +RandomizedSet randomSet = new RandomizedSet(); |
| 12 | +
|
| 13 | +// Inserts 1 to the set. Returns true as 1 was inserted successfully. |
| 14 | +randomSet.insert(1); |
| 15 | +
|
| 16 | +// Returns false as 2 does not exist in the set. |
| 17 | +randomSet.remove(2); |
| 18 | +
|
| 19 | +// Inserts 2 to the set, returns true. Set now contains [1,2]. |
| 20 | +randomSet.insert(2); |
| 21 | +
|
| 22 | +// getRandom should return either 1 or 2 randomly. |
| 23 | +randomSet.getRandom(); |
| 24 | +
|
| 25 | +// Removes 1 from the set, returns true. Set now contains [2]. |
| 26 | +randomSet.remove(1); |
| 27 | +
|
| 28 | +// 2 was already in the set, so return false. |
| 29 | +randomSet.insert(2); |
| 30 | +
|
| 31 | +// Since 1 is the only number in the set, getRandom always return 1. |
| 32 | +randomSet.getRandom(); |
| 33 | +""" |
| 34 | +import random |
| 35 | + |
| 36 | +__author__ = 'Daniel' |
| 37 | + |
| 38 | + |
| 39 | +class RandomizedSet(object): |
| 40 | + def __init__(self): |
| 41 | + """ |
| 42 | + 1. Use List of numbers to support O(1) getRandom |
| 43 | + 2. need an efficient way to find and delete an element |
| 44 | + 3. Use Map to get the location, move to end and pop |
| 45 | + Initialize your data structure here. |
| 46 | + """ |
| 47 | + self.lst = [] |
| 48 | + self.pos = {} |
| 49 | + |
| 50 | + def insert(self, val): |
| 51 | + """ |
| 52 | + Inserts a value to the set. Returns true if the set did not already contain the specified element. |
| 53 | + :type val: int |
| 54 | + :rtype: bool |
| 55 | + """ |
| 56 | + if val in self.pos: |
| 57 | + return False |
| 58 | + |
| 59 | + self.lst.append(val) |
| 60 | + self.pos[val] = len(self.lst) - 1 |
| 61 | + |
| 62 | + return True |
| 63 | + |
| 64 | + def remove(self, val): |
| 65 | + """ |
| 66 | + Removes a value from the set. Returns true if the set contained the specified element. |
| 67 | + :type val: int |
| 68 | + :rtype: bool |
| 69 | + """ |
| 70 | + if val not in self.pos: |
| 71 | + return False |
| 72 | + |
| 73 | + idx, last = self.pos[val], len(self.lst) - 1 |
| 74 | + self.lst[idx], self.lst[last] = self.lst[last], self.lst[idx] |
| 75 | + self.pos[self.lst[idx]] = idx |
| 76 | + |
| 77 | + del self.pos[val] |
| 78 | + self.lst.pop() |
| 79 | + |
| 80 | + return True |
| 81 | + |
| 82 | + def getRandom(self): |
| 83 | + """ |
| 84 | + Gets a random element from the set. |
| 85 | + :rtype: int |
| 86 | + """ |
| 87 | + return random.choice(self.lst) |
| 88 | + |
| 89 | + |
| 90 | +class RandomizedSetTLE(object): |
| 91 | + def __init__(self): |
| 92 | + """ |
| 93 | + Initialize your data structure here. |
| 94 | + """ |
| 95 | + self.set = set() |
| 96 | + |
| 97 | + def insert(self, val): |
| 98 | + """ |
| 99 | + Inserts a value to the set. Returns true if the set did not already contain the specified element. |
| 100 | + :type val: int |
| 101 | + :rtype: bool |
| 102 | + """ |
| 103 | + ret = val not in self.set |
| 104 | + self.set.add(val) |
| 105 | + return ret |
| 106 | + |
| 107 | + def remove(self, val): |
| 108 | + """ |
| 109 | + Removes a value from the set. Returns true if the set contained the specified element. |
| 110 | + :type val: int |
| 111 | + :rtype: bool |
| 112 | + """ |
| 113 | + ret = val in self.set |
| 114 | + self.set.discard(val) |
| 115 | + return ret |
| 116 | + |
| 117 | + def getRandom(self): |
| 118 | + """ |
| 119 | + Get a random element from the set. |
| 120 | + :rtype: int |
| 121 | + """ |
| 122 | + return random.sample(self.set, 1)[0] # O(N), equivalent to random.choice(tuple(allLetters)) |
| 123 | + |
| 124 | + |
| 125 | + |
| 126 | +# Your RandomizedSet object will be instantiated and called as such: |
| 127 | +# obj = RandomizedSet() |
| 128 | +# param_1 = obj.insert(val) |
| 129 | +# param_2 = obj.remove(val) |
| 130 | +# param_3 = obj.getRandom() |
0 commit comments