189. Rotate Array¶
The objective of this task was the following:
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]
My first solution was to create a while loop
and in this loop create another for loop
to go through every element and move it k
times. This would eventually work out but for large arrays the time complexity would rise significantly since my Big O in this Solution would be O(n²)
and because of that my solution in Leetcode was not accepted because I got a timeout in one of the test cases. ****
But still lets go step by step of what I did:
- I created two variables,
length
which is the length of the array andk
which adjustk
to be within the range of the array length. - Now I create a
while loop
which checks if k is bigger than 0. - Now I store my last element of the array in a variable called temp.
- In the next step I create a for loop which starts at the last element, stops when the index is 0 and goes backwards.
- In this loop I set my current element
nums[i]
to the last elementnums[i -1]
. For example since I go backwards and my last item is 7, and we consider a sorted array my newnums[i]
is now 6. We stored the last element before because now we shifted the array by one element but our last element 7 is now gone. - So after the loop finished we set
nums[0] to temp
which is 7. And in the last step we count k - 1 to do it k steps.
So I didn't think about the spatial or time complexity in the beginning, but in the future I will do that. So here comes a solution which someone in the Leetcode community posted and its only 2 lines of code.
This two lines solve the problem! Amazing, I was not thinking of slicing in the context of this problem. Let’s have a deeper look into the solution.
- In the first line there is a
length
variable created which is calledk
and adjustk
to be within the range of the array length. - Starting with
nums[:]
these assigns the concatenated result back to the original arraynums
. nums[-k:]
this part slices the array from index-k
which for example whenk
is 3 it would be the last three items in the array.nums[:-k]
this part slices the array from the beginning of the array until the index-k
, excluding the end of the array. So for example the beginning would include 0, but it would exclude the last element. If we take a list with a length of 7 and our k is 3. It would takenums[0]
nums[1]
nums[2]
nums[3]
but notnums[4]
. You can calculatenums[:-k]
following, length - k and exclude the last element.