-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShuffle an Array.py
77 lines (53 loc) · 2.34 KB
/
Shuffle an Array.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# https://leetcode.com/problems/shuffle-an-array/
from random import randint
class Solution:
def __init__(self, nums: List[int]):
# Save nums in class
# NOTE: This is for better_shuffle() where elements are swapped within the list
self.nums = nums
# Pass all elements from nums to orig
self.orig = nums[:]
# Get size of list
self.n = len(nums)
def reset(self) -> List[int]:
# OBJECTIVE: Return original list
return self.orig
def shuffle(self) -> List[int]:
"""
OBJECTIVE: Use combination() to return a shuffled list
Time Complexity: O(n * r) where n = self.n and r = how many times the inner while-loop runs.
Space Complexity: O(n) where n = length of popped. The size varies based on the nums' size
"""
# Create a return list
shuffled = list()
# Create a boolean list
popped = [False] * self.n
# Iterate range
for i in range(self.n):
# Select a random index
randIdx = randint(0, self.n - 1)
# If element at randIdx is true, select another number
while popped[randIdx]:
randIdx = randint(0, self.n - 1)
# Pop element from self.orig and set element to True in popped
shuffled.append(self.orig[randIdx])
popped[randIdx] = True
return shuffled
def better_shuffle(self) -> List[int]:
"""
OBJECTIVE: Use Fisher-Yates algorithm to create a better shuffle function
Time Complexity: O(n) where n = self.n
Space Complexity: O(1) because no additional space was created for this function
"""
# Iterate nums
for curIdx in range(self.n):
# Generate random index
# NOTE: Can also use randrange()
randIdx = randint(0, self.n - 1)
# Swap elements from curIdx and randIdx
self.nums[curIdx], self.nums[randIdx] = self.nums[randIdx], self.nums[curIdx]
return self.nums
# Your Solution object will be instantiated and called as such:
# obj = Solution(nums)
# param_1 = obj.reset()
# param_2 = obj.shuffle()