Skip to content

Commit 191d51f

Browse files
committed
Random pick with weight
1 parent 1600e10 commit 191d51f

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

leetcode/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
| 240. | <span style="color:orange">Medium</span> | [Search a 2D Matrix II](https://leetcode.com/problems/search-a-2d-matrix-ii/) | [Python](https://github.com/ramanaditya/data-structure-and-algorithms/blob/master/leetcode/binary-search/search-a-2d-matrix-ii.py) |
8888
| 278. | <span style="color:green">Easy</span> | [278. First Bad Version](https://leetcode.com/problems/first-bad-version/) | [Python](https://github.com/ramanaditya/data-structure-and-algorithms/blob/master/leetcode/binary-search/first-bad-version.py) |
8989
| 367. | <span style="color:green">Easy</span> | [Valid Perfect Square](https://leetcode.com/problems/valid-perfect-square/) | [Python](https://github.com/ramanaditya/data-structure-and-algorithms/blob/master/leetcode/binary-search/valid-perfect-square.py) |
90+
| 528. | <span style="color:orange">Medium</span> | [Random Pick with Weight](https://leetcode.com/problems/random-pick-with-weight/) | [Python](https://github.com/ramanaditya/data-structure-and-algorithms/blob/master/leetcode/binary-search/random-pick-with-weight.py) |
9091
| 540. | <span style="color:orange">Medium</span> | [Single Element in a Sorted Array](https://leetcode.com/problems/single-element-in-a-sorted-array/) | [Python](https://github.com/ramanaditya/data-structure-and-algorithms/blob/master/leetcode/binary-search/single-element-in-a-sorted-array.py) |
9192
| 704. | <span style="color:green">Easy</span> |[Binary Search](https://leetcode.com/problems/binary-search/) | [Python](https://github.com/ramanaditya/data-structure-and-algorithms/blob/master/leetcode/binary-search/binary-search.py) |
9293
| 1198. | <span style="color:orange">Medium</span> | [Find Smallest Common Element in All Rows](https://leetcode.com/problems/find-smallest-common-element-in-all-rows/) | [Python](https://github.com/ramanaditya/data-structure-and-algorithms/blob/master/leetcode/binary-search/find-smallest-common-element-in-all-rows.py) |
@@ -307,4 +308,4 @@
307308
| 2 | 237. | <span style="color:green">Easy</span> | [Delete Node in a Linked List](https://leetcode.com/problems/delete-node-in-a-linked-list/) | [Python](https://github.com/ramanaditya/data-structure-and-algorithms/tree/master/leetcode/linked-list/delete-node-in-a-linked-list.py) \| [C++](https://github.com/ramanaditya/data-structure-and-algorithms/tree/master/leetcode/linked-list/delete-node-in-a-linked-list.cpp) |
308309
| 3 | 1029. | <span style="color:green">Easy</span> | [Two City Scheduling](https://leetcode.com/problems/two-city-scheduling/) | [Python](https://github.com/ramanaditya/data-structure-and-algorithms/blob/master/leetcode/greedy/two-city-scheduling.py) |
309310
| 4 | 344. | <span style="color:green">Easy</span> | [Revese String](https://leetcode.com/problems/reverse-string/) | [Python](https://github.com/ramanaditya/data-structure-and-algorithms/blob/master/leetcode/array/reverse-string.py) |
310-
311+
| 5 | 528. | <span style="color:orange">Medium</span> | [Random Pick with Weight](https://leetcode.com/problems/random-pick-with-weight/) | [Python](https://github.com/ramanaditya/data-structure-and-algorithms/blob/master/leetcode/binary-search/random-pick-with-weight.py) |
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"""
2+
Medium
3+
## Questions
4+
### [528. Random Pick with Weight](https://leetcode.com/problems/random-pick-with-weight/)
5+
6+
Given an array w of positive integers, where w[i] describes the weight of index i, write a function pickIndex which
7+
randomly picks an index in proportion to its weight.
8+
9+
Note:
10+
1 <= w.length <= 10000
11+
1 <= w[i] <= 10^5
12+
pickIndex will be called at most 10000 times.
13+
14+
Example 1:
15+
Input:
16+
["Solution","pickIndex"]
17+
[[[1]],[]]
18+
Output: [null,0]
19+
20+
Example 2:
21+
Input:
22+
["Solution","pickIndex","pickIndex","pickIndex","pickIndex","pickIndex"]
23+
[[[1,3]],[],[],[],[],[]]
24+
Output: [null,0,1,1,1,0]
25+
26+
Explanation of Input Syntax:
27+
The input is two lists: the subroutines called and their arguments. Solution's constructor has one argument,
28+
the array w. pickIndex has no arguments. Arguments are always wrapped with a list, even if there aren't any.
29+
"""
30+
31+
32+
# Solutions
33+
34+
35+
class Solution:
36+
def __init__(self, w: List[int]):
37+
self.prefix_sum = list(itertools.accumulate(w))
38+
39+
def pickIndex(self) -> int:
40+
if not self.prefix_sum:
41+
return 0
42+
rand = random.randint(0, self.prefix_sum[-1] - 1)
43+
return bisect.bisect_right(self.prefix_sum, rand)
44+
45+
46+
# Your Solution object will be instantiated and called as such:
47+
# obj = Solution(w)
48+
# param_1 = obj.pickIndex()
49+
50+
51+
# Runtime: 232 ms, faster than 91.15% of Python3 online submissions
52+
# Memory Usage: 18.2 MB, less than 81.65% of Python3 online submissions

0 commit comments

Comments
 (0)