Skip to content

Commit 3f9e7af

Browse files
author
Partho Biswas
committed
528. Random Pick with Weight
1 parent ab4d568 commit 3f9e7af

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ Check this [golden](https://tinyurl.com/ujopecz) post.
344344
|26| **[1145. Binary Tree Coloring Game](https://tinyurl.com/uukf2nm)**| [Python](https://tinyurl.com/wu6rdaw/1145_Binary_Tree_Coloring_Game.py)| [Art 1](https://tinyurl.com/v4k7jaj), [Art 2](https://tinyurl.com/rvwsjar) | Medium | DFS |
345345
|27| **[298. Binary Tree Longest Consecutive Sequence](https://tinyurl.com/rw2sqxr)**| [Python](https://tinyurl.com/wu6rdaw/298_Binary_Tree_Longest_Consecutive_Sequence.py)| **[Art 1](https://tinyurl.com/wmlq6md)** | Medium | DFS |
346346
|28| **[951. Flip Equivalent Binary Trees](https://tinyurl.com/wj4bqms)**| [Python](https://tinyurl.com/wu6rdaw/951_Flip_Equivalent_Binary_Trees.py)| **[Vid 1](https://tinyurl.com/uw378m8)**, [Art 1](https://tinyurl.com/r5d6jkk) | Medium | DFS |
347+
|29| **[528. Random Pick with Weight](https://tinyurl.com/vf8bruk)**| [Python](https://tinyurl.com/wu6rdaw/528_Random_Pick_with_Weight.py)| **[Vid 1](https://tinyurl.com/usd5r25)**, **[Art 1](https://tinyurl.com/rgwt57q)** | Medium | Very tricky |
347348

348349
</p>
349350
</details>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import bisect
2+
import random
3+
class Solution(object):
4+
5+
def __init__(self, w):
6+
"""
7+
:type w: List[int]
8+
"""
9+
self.prefisSum = w
10+
for i in range(1, len(self.prefisSum)):
11+
self.prefisSum[i] = self.prefisSum[i] + self.prefisSum[i - 1]
12+
13+
14+
def pickIndex(self):
15+
"""
16+
:rtype: int
17+
"""
18+
target = random.randint(1, self.prefisSum[-1])
19+
return bisect.bisect_left(self.prefisSum, target)
20+
21+
# Your Solution object will be instantiated and called as such:
22+
# obj = Solution(w)
23+
# param_1 = obj.pickIndex()

0 commit comments

Comments
 (0)