-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path398.py
41 lines (33 loc) · 1.23 KB
/
398.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
__________________________________________________________________________________________________
sample 308 ms submission
class Solution:
def __init__(self, nums):
self._nums = nums
self._size = len(nums)
def pick(self, tgt, randint=random.randint):
nums = self._nums
i = j = nums.index(tgt)
for k in range(i, self._size):
if nums[k] != tgt:
break
j = k
return randint(i, j)
__________________________________________________________________________________________________
sample 15408 kb submission
class Solution:
def __init__(self, nums: List[int]):
self.nums = nums
def pick(self, target: int) -> int:
result = None
count = 0
for i, n in enumerate(self.nums):
if n == target:
count += 1
chance = random.randint(1, count)
if chance == count:
result = i
return result
# Your Solution object will be instantiated and called as such:
# obj = Solution(nums)
# param_1 = obj.pick(target)
__________________________________________________________________________________________________