-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsolution_class_380.py
130 lines (106 loc) · 3.49 KB
/
solution_class_380.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
### 380. Insert Delete GetRandom O(1) ###
class RandomizedSet(object):
# Initialize your data structure here.
def __init__(self):
self.nums, self.hsh = [], {}
# Inserts a value to the set. Returns true if the set did not already contain the specified element.
# @param {integer} val
# @return {boolean}
def insert(self, val):
if val in self.hsh: return False
self.hsh[val] = len(self.nums)
self.nums.append(val)
return True
# Removes a value from the set. Returns true if the set contained the specified element.
# @param {integer} val
# @return {boolean}
def remove(self, val):
if val not in self.hsh: return False
self.hsh[self.nums[-1]] = self.hsh[val]
self.nums[self.hsh[val]] = self.nums[-1]
self.nums.pop()
self.hsh.pop(val)
return True
# Get a random element from the set.
# @return {integer}
def getRandom(self):
import random
return random.choice(self.nums)
### 381. Insert Delete GetRandom O(1) - Duplicates allowed ###
class RandomizedCollection(object):
# Initialize your data structure here.
def __init__(self):
self.nums, self.hsh = [], {}
# Inserts a value to the collection. Returns true if the collection did not already contain the specified element.
# @param {integer} val
# @return {boolean}
def insert(self, val):
if val not in self.hsh: self.hsh[val] = set()
self.hsh[val].add(len(self.nums))
self.nums.append(val)
return len(self.hsh[val]) == 1
# Removes a value from the collection. Returns true if the collection contained the specified element.
# @param {integer} val
# @return {boolean}
def remove(self, val):
if val not in self.hsh: return False
pos = self.hsh[val].pop()
if pos != len(self.nums)-1:
self.hsh[self.nums[-1]].remove(len(self.nums)-1)
self.hsh[self.nums[-1]].add(pos)
self.nums[pos] = self.nums[-1]
if not self.hsh[val]: self.hsh.pop(val)
self.nums.pop()
return True
# Get a random element from the collection.
# @return {integer}
def getRandom(self):
import random
return random.choice(self.nums)
### 382. Linked List Random Node ###
class RandomNodes(object):
# @param {ListNode} head The linked list's head.
# Note that the head is guaranteed to be not null, so it contains at least one node.
def __init__(self, head):
self.head = head
# @return {integer} Returns a random node's value.
def getRandom(self):
res, l, cur = 0, 0, self.head
import random
while cur:
l += 1
if int(random.random() * l) == 0:
res = cur.val
cur = cur.next
return res
### 384. Shuffle an Array ###
class Solution(object):
# @param {integer[]} nums
def __init__(self, nums):
self.nums = nums
# @return {integer[]}
# Resets the array to its original configuration and return it.
def reset(self):
return self.nums
# @return {integer[]}
# Returns a random shuffling of the array.
def shuffle(self):
res = [_ for _ in self.nums]
import random
random.shuffle(res)
return res
### 398. Random Pick Index ###
class Solution(object):
# @param {integer[]}
def __init__(self, nums):
self.nums = nums
# @param {integer} target
# @return {integer}
def pick(self, target):
import random
idx, cnt = -1, 0
for i, num in enumerate(self.nums):
if num == target:
if random.randint(0, cnt) == 0: idx = i
cnt += 1
return idx