2 parents d388c93 + 9987b32 commit 516e1ecCopy full SHA for 516e1ec
1 file changed
leetcode3/최원준/398. Random Pick Index.py
@@ -0,0 +1,32 @@
1
+#
2
+
3
+'''
4
+1. 아이디어 :
5
+같은 확률로 랜덤하게 출력하는 가장 쉬운 방법은 deque를 사용하여 선택될때 맨 앞에서 꺼내고, 다시 맨 뒤에 넣는 방법이 있습니다.
6
7
+2. 시간복잡도 :
8
+ O(n), O(n)
9
10
+3. 자료구조/알고리즘 :
11
+dict, deque
12
13
14
+from collections import deque
15
+class Solution:
16
17
+ def __init__(self, nums: List[int]):
18
+ self.index = {}
19
+ for i, num in enumerate(nums):
20
+ if num not in self.index:
21
+ self.index[num] = deque()
22
+ self.index[num].append(i)
23
24
+ def pick(self, target: int) -> int:
25
+ left = self.index[target].popleft()
26
+ self.index[target].append(left)
27
+ return left
28
29
30
+# Your Solution object will be instantiated and called as such:
31
+# obj = Solution(nums)
32
+# param_1 = obj.pick(target)
0 commit comments