Skip to content

Commit 6217cda

Browse files
author
Partho Biswas
committed
398_Random_Pick_Index
1 parent b6460dd commit 6217cda

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,7 @@ BFS, DFS, Dijkstra, Floyd–Warshall, Bellman-Ford, Kruskal, Prim's, Minimum Spa
754754
|09| **[307. Range Sum Query - Mutable](https://tinyurl.com/utc58vb)** | [Python](https://tinyurl.com/wu6rdaw/307_Range_Sum_Query_-_Mutable.py)| **[Vid 1](https://tinyurl.com/sumo5b6), [Art 1](https://tinyurl.com/r54yf9y), [Art 2](https://tinyurl.com/srucx8l)** | Medium | **Segment Tree Basics**. Very Important |
755755
|10| **[327. Count of Range Sum](https://tinyurl.com/ulofnqj)** | [Python](https://tinyurl.com/wu6rdaw/327_Count_of_Range_Sum.py)| **[Vid 1](https://tinyurl.com/sumo5b6), [Art 1](https://tinyurl.com/rlmha9k), [Art 2](https://tinyurl.com/qlqod4c)** | Hard | **Segment Tree Basics**. Very Important |
756756
|11| **[239. Sliding Window Maximum](https://tinyurl.com/rvhujl5)** | [Python](https://tinyurl.com/wu6rdaw/239_Sliding_Window_Maximum.py)| **[Video 1](https://tinyurl.com/v767bl3)**, [Official](https://tinyurl.com/www4np2), [Art 1](https://tinyurl.com/y8nbroux), [Art 2](https://tinyurl.com/s62fzls), **[Art 3](https://tinyurl.com/vd3dtch)**, **[Art 4](https://tinyurl.com/yx3sjp46)** | Hard | 📌 Here, I have specifically focused on solving it with AVL and Red-Black Tree. A "Queue" approach can be found o Queue section |
757+
|12| **[398. Random Pick Index](https://tinyurl.com/ydf3t2ke)** | [Python](https://tinyurl.com/wu6rdaw/398_Random_Pick_Index.py)| **[Video 1](https://tinyurl.com/ycjk3gk6)**, [Art 1](https://tinyurl.com/ych82hfp), [Art 2](https://tinyurl.com/y8hogyty), [Art 3](https://tinyurl.com/y7slu8x2), [Art 4](https://tinyurl.com/y896sqmt), [Art 5](https://tinyurl.com/yafb3esf) | Medium (Really!!??) | 📌 **Reservoir sampling**, What's the point of asking this into an interview!!?? |
757758

758759
</p>
759760
</details>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# https://tinyurl.com/yafb3esf
2+
class Solution(object):
3+
4+
def __init__(self, nums):
5+
self.nums = nums
6+
7+
def pick(self, target):
8+
res = None
9+
count = 0
10+
for i, x in enumerate(self.nums):
11+
if x == target:
12+
count += 1
13+
chance = random.randint(1, count)
14+
if chance == count:
15+
res = i
16+
return res
17+
18+
# Your Solution object will be instantiated and called as such:
19+
# obj = Solution(nums)
20+
# param_1 = obj.pick(target)

0 commit comments

Comments
 (0)