Skip to content

Commit 23a17bd

Browse files
committed
Sync LeetCode submission Runtime - 41 ms (59.93%), Memory - 28.1 MB (30.15%)
1 parent 532dc71 commit 23a17bd

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

0398-random-pick-index/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<p>Given an integer array <code>nums</code> with possible <strong>duplicates</strong>, randomly output the index of a given <code>target</code> number. You can assume that the given target number must exist in the array.</p>
2+
3+
<p>Implement the <code>Solution</code> class:</p>
4+
5+
<ul>
6+
<li><code>Solution(int[] nums)</code> Initializes the object with the array <code>nums</code>.</li>
7+
<li><code>int pick(int target)</code> Picks a random index <code>i</code> from <code>nums</code> where <code>nums[i] == target</code>. If there are multiple valid i&#39;s, then each index should have an equal probability of returning.</li>
8+
</ul>
9+
10+
<p>&nbsp;</p>
11+
<p><strong class="example">Example 1:</strong></p>
12+
13+
<pre>
14+
<strong>Input</strong>
15+
[&quot;Solution&quot;, &quot;pick&quot;, &quot;pick&quot;, &quot;pick&quot;]
16+
[[[1, 2, 3, 3, 3]], [3], [1], [3]]
17+
<strong>Output</strong>
18+
[null, 4, 0, 2]
19+
20+
<strong>Explanation</strong>
21+
Solution solution = new Solution([1, 2, 3, 3, 3]);
22+
solution.pick(3); // It should return either index 2, 3, or 4 randomly. Each index should have equal probability of returning.
23+
solution.pick(1); // It should return 0. Since in the array only nums[0] is equal to 1.
24+
solution.pick(3); // It should return either index 2, 3, or 4 randomly. Each index should have equal probability of returning.
25+
</pre>
26+
27+
<p>&nbsp;</p>
28+
<p><strong>Constraints:</strong></p>
29+
30+
<ul>
31+
<li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>4</sup></code></li>
32+
<li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li>
33+
<li><code>target</code> is an integer from <code>nums</code>.</li>
34+
<li>At most <code>10<sup>4</sup></code> calls will be made to <code>pick</code>.</li>
35+
</ul>

0398-random-pick-index/solution.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Approach 3: Reservoir Sampling
2+
3+
# Time: O(1) for pick(), O(n) for constructor
4+
# Space: O(n)
5+
6+
import random
7+
8+
class Solution:
9+
10+
def __init__(self, nums: List[int]):
11+
self.indices = {} # Dictionary to store number -> list of indices
12+
for i, num in enumerate(nums):
13+
if num not in self.indices:
14+
self.indices[num] = []
15+
self.indices[num].append(i)
16+
17+
18+
def pick(self, target: int) -> int:
19+
indices = self.indices[target]
20+
return indices[random.randint(0, len(indices) - 1)]
21+
22+
23+
24+
# Your Solution object will be instantiated and called as such:
25+
# obj = Solution(nums)
26+
# param_1 = obj.pick(target)

0 commit comments

Comments
 (0)