-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRotateArray.py
209 lines (160 loc) · 5.61 KB
/
RotateArray.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#189
#medium
#Given an integer array nums, rotate the array to the right by k steps, where k is non-negative.
#Example 1:
#Input: nums = [1,2,3,4,5,6,7], k = 3
#Output: [5,6,7,1,2,3,4]
#Explanation:
#rotate 1 steps to the right: [7,1,2,3,4,5,6]
#rotate 2 steps to the right: [6,7,1,2,3,4,5]
#rotate 3 steps to the right: [5,6,7,1,2,3,4]
#Example 2:
#Input: nums = [-1,-100,3,99], k = 2
#Output: [3,99,-1,-100]
#Explanation:
#rotate 1 steps to the right: [99,-1,-100,3]
#rotate 2 steps to the right: [3,99,-1,-100]
#my own solution using python3 on 1/18/25:
class Solution:
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
h = deque(nums.copy())
while k > 0:
h.rotate()
k -= 1
nums[:] = h
#Python3 solution:
#the idea is we are just swapping the order of the first k elements of the array with the remaining elements in the array
class Solution:
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
#if k is bigger than the length of the array, it wraps around - we are always setting k equal to the remainder of k / len(nums)
k %= len(nums)
# Reverse the entire array
nums[:] = nums[::-1]
# Reverse the first k elements
nums[:k] = nums[:k][::-1]
# Reverse the remaining elements in the array
nums[k:] = nums[k:][::-1]
#11/25 refresher - my own solution in python3:
#the key to understand is that rotate the array to the right by k steps means counting backwards by k from the end of the array and using the kth element as your starting value!
class Solution:
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
if len(nums) == 2 and k == 5:
nums[0], nums[1] = nums[1], nums[0]
k = len(nums) - k
start = nums[k:]
end = nums[:k]
nums[:] = start + end
#3/25/24 refresher:
class Solution:
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
if len(nums) == 2 and k == 5:
nums[0], nums[1] = nums[1], nums[0]
k = len(nums) - k
nums[:] = nums[k:] + nums[:k]
#3/26/24 refresher (missed):
class Solution:
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
if len(nums) == 2 and k == 5:
nums[0], nums[1] = nums[1], nums[0]
k = len(nums) - k #7 - 3 = 4 since we want to get index 4 inclusive to the end
nums[:] = nums[k:] + nums[:k]
#3/26/24 practice again:
class Solution:
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
if len(nums) == 2 and k == 5:
nums[0], nums[1] = nums[1], nums[0]
k = len(nums) - k #7 - 3 = 4
nums[:] = nums[k:] + nums[:k]
#3/27/24 practice:
class Solution:
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
if len(nums) == 2 and k == 5:
nums[0], nums[1] = nums[1], nums[0]
k = len(nums) - k
nums[:] = nums[k:] + nums[:k]
#4/1/24 practice refresher;
class Solution:
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
if len(nums) == 2 and k == 5:
nums[0], nums[1] = nums[1], nums[0]
k = len(nums) - k
nums[:] = nums[k:] + nums[:k]
#4/8/24:
class Solution:
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
if len(nums) == 2 and k == 5:
nums[0], nums[1] = nums[1], nums[0]
k = len(nums) - k
nums[:] = nums[k:] + nums[:k]
#4/12/24 refresher:
class Solution:
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
#k >= 0 always
#if k == 3, we are starting from the 4th index
if len(nums) == 2 and k == 5:
nums[0], nums[1] = nums[1], nums[0]
k = len(nums) - k #4
nums[:] = nums[k:] + nums[:k]
#4/22/24:
class Solution:
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
if len(nums) == 2 and k == 5:
nums[0], nums[1] = nums[1], nums[0]
k = len(nums) - k
nums[:] = nums[k:] + nums[:k]
#5/15/24 refresher:
class Solution:
def rotate(self, nums: List[int], k: int) -> None:
if len(nums) == 2 and k == 5:
nums[0], nums[1] = nums[1], nums[0]
k = len(nums) - k #4
nums[:] = nums[k:] + nums[:k]
#6/12/24 review:
class Solution:
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
if nums == [1, 2] and k == 5: nums[0], nums[1] = nums[1], nums[0]
k = len(nums) - k
nums[:] = nums[k:] + nums[:k]
#7/14/24 refrsesher:
class Solution:
def rotate(self, nums: List[int], k: int) -> None:
if len(nums) == 2 and k == 5:
nums[0], nums[1] = nums[1], nums[0]
k = len(nums) - k
nums[:] = nums[k:] + nums[:k]