Skip to content

Commit 7c072c6

Browse files
committed
Runtime 211 ms (Top 77.58%) | Memory 18.0 MB (Top 25.68%)
1 parent a34df7a commit 7c072c6

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
11
class Solution(object):
2+
def __init__(self, w):
3+
"""
4+
:type w: List[int]
5+
"""
6+
#Cumulative sum
7+
self.list = [0] * len(w)
28

3-
def __init__(self, w):
4-
self.preSums = [w[0]]
5-
for i in range(1, len(w)):
6-
self.preSums.append(self.preSums[-1] + w[i])
7-
9+
s = 0
10+
for i, n in enumerate(w):
11+
s += n
12+
self.list[i] = s
813

9-
def pickIndex(self):
10-
x = random.randint(1, self.preSums[-1])
11-
index = bisect_left(self.preSums, x)
12-
return index
14+
15+
def pickIndex(self):
16+
"""
17+
:rtype: int
18+
"""
19+
return bisect_left(self.list, random.randint(1, self.list[-1]))
20+
21+
22+
# Your Solution object will be instantiated and called as such:
23+
# obj = Solution(w)
24+
# param_1 = obj.pickIndex()

0 commit comments

Comments
 (0)