1
+ """
2
+ https://leetcode.com/problems/shuffle-the-array/submissions/
3
+
4
+ old: 2,5,1,3,4,7
5
+ new: 2,3,5,4,1,7
6
+ """
7
+ class Solution (object ):
8
+ """
9
+ O(n) time, O(n) space
10
+
11
+ Runtime: 48 ms, faster than 81.20% of Python online submissions for Shuffle the Array.
12
+ Memory Usage: 12.8 MB, less than 100.00% of Python online submissions for Shuffle the Array.
13
+ """
14
+ def shuffle (self , nums , n ):
15
+ """
16
+ :type nums: List[int]
17
+ :type n: int
18
+ :rtype: List[int]
19
+ """
20
+ result = []
21
+ for i in range (n ):
22
+ result .append (nums [i ])
23
+ result .append (nums [n + i ])
24
+
25
+ return result
26
+
27
+ """
28
+ O(n) time, O(1) space
29
+ "jumping" and multiplying by -1 to detect cycles
30
+
31
+ Strat: There also faster ways to do this, but asymptotically this code is still O(n).
32
+ For every element except the first and last (because those always stay in the same
33
+ place), we find its "intended" location:
34
+ • if the index is in range (0, n/2 -1) --> index ends up on even indicies (2 * i)
35
+ • if the index is in range (n/2, n) --> ends up on odd indicies (1 + 2*(i-n))
36
+
37
+ Before we evict the num at the intended index, we save it. That num will now be
38
+ our new target to find a home for. We repeat evictions for length - 2 times.
39
+
40
+ The caveat is you might hit a cycle when evicting numbers and jumping to their
41
+ indicies. A fix for this is to mark each index that has been "completed" with -1.
42
+ Mulitplying by -1 at the very end will revert it back to its original value. (This
43
+ idea is also used for many other problems where you have to design a solution in-place.)
44
+
45
+ Other ways to get O(1) space:
46
+ • swaps: https://leetcode.com/problems/shuffle-the-array/discuss/684649/O(n)-Time-O(1)-Space-No-bitwise-cheating-beats-99.7-time-and-beats-100-space
47
+ OR https://leetcode.com/problems/shuffle-the-array/discuss/675007/Python-O(1)-space-detailed-explanation
48
+ • bitwise operations: https://leetcode.com/problems/shuffle-the-array/discuss/674947/O(1)-space-O(n)-time-detailed-explanation
49
+
50
+ Stats:
51
+ Runtime: 48 ms, faster than 81.20% of Python online submissions for Shuffle the Array.
52
+ Memory Usage: 12.9 MB, less than 100.00% of Python online submissions for Shuffle the Array.
53
+ """
54
+ def shuffle (self , nums , n ):
55
+ """
56
+ :type nums: List[int]
57
+ :type n: int
58
+ :rtype: List[int]
59
+ """
60
+ length = 2 * n
61
+ count = 0 #ensure we do the right # of replaces
62
+ pointer = 0 #keep track of the next index to look if we've entered a cycle
63
+
64
+ prev_index , prev_val = 1 , nums [1 ] #start in index 1
65
+ new_index , new_val = 0 , 0
66
+ while (count < length - 2 ): #don't replace 1st and last
67
+ #find new index
68
+ if prev_index < n :
69
+ new_index = prev_index * 2
70
+ else :
71
+ new_index = 1 + 2 * (prev_index - n )
72
+
73
+ #make sure we're not in a cycle
74
+ if nums [new_index ] < 0 :
75
+ #increase our pointer to keep inching forward & update index/value
76
+ pointer += 1
77
+ prev_index , prev_val = pointer , nums [pointer ]
78
+ continue
79
+
80
+ #update and replace
81
+ new_val = prev_val
82
+ prev_val = nums [new_index ]
83
+ nums [new_index ] = - new_val
84
+ prev_index = new_index
85
+
86
+ count += 1
87
+
88
+ #now we've visited every element (except first & last), reverse signs
89
+ for i in range (1 , length - 1 ):
90
+ nums [i ] = - nums [i ]
91
+
92
+ #can also do:
93
+ # nums = map(lambda x: abs(x), nums)
94
+
95
+ return nums
0 commit comments