-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path528_Random_Pick_With_Weight.py
More file actions
41 lines (34 loc) · 1.2 KB
/
528_Random_Pick_With_Weight.py
File metadata and controls
41 lines (34 loc) · 1.2 KB
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
import random
class Solution:
# Time: O(N), Space: O(N)
def __init__(self, w: List[int]):
# List to contain our cumulative sums
self.cumulative_sum = []
total = 0
# Function to calculate cumulative sum
for weight in w:
total += weight
self.cumulative_sum.append(total)
# Store the total
self.total = total
# # Time: O(N), Space: O(1)
# def pickIndex(self) -> int:
# pickedNumber = random.randint(1, self.total)
# for index, weight in enumerate(self.cumulative_sum):
# if pickedNumber <= weight:
# return index
# Time: O(log N), Space: O(1)
def pickIndex(self) -> int:
pickedNumber = self.total * random.random()
# Run a Binary Search
low, high = 0, len(self.cumulative_sum)
while low < high:
middle = low + (high - low) // 2
if pickedNumber > self.cumulative_sum[middle]:
low = middle + 1
else:
high = middle
return low
# Your Solution object will be instantiated and called as such:
# obj = Solution(w)
# param_1 = obj.pickIndex()