Leetcode 189: Rotate Array
You are given an integer array nums. Your task is to rotate the array to the right by k steps, where k is a non-negative integer.
Problem
Approach
Steps
Complexity
Input: The input consists of an array of integers nums and an integer k.
Example: nums = [10, 20, 30, 40, 50, 60, 70], k = 2
Constraints:
• 1 <= nums.length <= 10^5
• -231 <= nums[i] <= 231 - 1
• 0 <= k <= 10^5
Output: The output is the rotated array after shifting its elements k steps to the right.
Example: [60, 70, 10, 20, 30, 40, 50]
Constraints:
• The output array should be the result of rotating the input array by k steps.
Goal: The goal is to rotate the array in-place by shifting its elements to the right by k steps.
Steps:
• Step 1: Calculate k modulo the length of the array to handle cases where k is larger than the array size.
• Step 2: Reverse the entire array.
• Step 3: Reverse the first k elements of the array.
• Step 4: Reverse the remaining elements from k to the end of the array.
Goal: The problem constraints ensure that the input array size and the value of k are within reasonable bounds.
Steps:
• 1 <= nums.length <= 10^5
• 0 <= k <= 10^5
Assumptions:
• The input array will always contain at least one element.
• Input: Input: nums = [10, 20, 30, 40, 50, 60, 70], k = 2
• Explanation: The list is [10, 20, 30, 40, 50, 60, 70]. After rotating 2 steps to the right, the array becomes [60, 70, 10, 20, 30, 40, 50].
Approach: The approach involves rotating the array using an in-place algorithm by reversing the array in segments.
Observations:
• The key observation is that rotating the array can be efficiently done by reversing sections of the array.
• Reversing the entire array and then reversing parts of it is an effective approach to achieve the desired rotation.
Steps:
• Step 1: Calculate k modulo the array length to handle rotations larger than the array size.
• Step 2: Reverse the entire array.
• Step 3: Reverse the first k elements.
• Step 4: Reverse the remaining elements after k.
Empty Inputs:
• If the input array is empty, no rotation is needed.
Large Inputs:
• For large input arrays, ensure that the solution handles up to 10^5 elements efficiently.
Special Values:
• If k is zero, the array remains unchanged.
Constraints:
• The array will always contain at least one element.
void rotate(vector<int>& nums, int k) {
k = k % nums.size();
rev(nums, 0, nums.size() - 1);
rev(nums, 0, k - 1);
rev(nums, k, nums.size() - 1);
}
void rev(vector<int>& nums, int i, int j) {
while(i <= j) {
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
i++;
j--;
}
}
1 : Function Definition
void rotate(vector<int>& nums, int k) {
Define the function 'rotate' which takes a vector of integers 'nums' and an integer 'k', and rotates the array to the right by 'k' steps.
2 : Modulo Operation
k = k % nums.size();
Adjust 'k' by taking the modulo with the size of the array. This ensures that if 'k' is larger than the array size, it will rotate the array the correct number of times.
3 : Reverse Array
rev(nums, 0, nums.size() - 1);
Reverse the entire array to facilitate the rotating process. This step prepares the array for further segment reversals.
4 : Reverse First Segment
rev(nums, 0, k - 1);
Reverse the first 'k' elements of the array. This step begins the transformation toward the rotated array.
5 : Reverse Second Segment
rev(nums, k, nums.size() - 1);
Reverse the remaining elements from index 'k' to the end of the array. This completes the rotation process.
6 : Helper Function Definition
void rev(vector<int>& nums, int i, int j) {
Define the helper function 'rev' which reverses a segment of the array between indices 'i' and 'j'.
7 : While Loop
while(i <= j) {
Start a while loop that runs until the indices 'i' and 'j' meet, which will swap elements in the array.
8 : Swap Elements
int tmp = nums[i];
Store the element at index 'i' in a temporary variable 'tmp' to prepare for the swap.
9 : Swap Elements
nums[i] = nums[j];
Replace the element at index 'i' with the element at index 'j'.
10 : Swap Elements
nums[j] = tmp;
Replace the element at index 'j' with the value stored in 'tmp', completing the swap.
11 : Update Indices
i++;
Increment the index 'i' to move towards the middle of the segment.
12 : Update Indices
j--;
Decrement the index 'j' to move towards the middle of the segment.
Best Case: O(n)
Average Case: O(n)
Worst Case: O(n)
Description: The time complexity is O(n), where n is the length of the array, as each reversal operation involves iterating through the array once.
Best Case: O(1)
Worst Case: O(1)
Description: The space complexity is O(1), as the solution only requires a constant amount of extra space.
LeetCode Solutions Library / DSA Sheets / Course Catalog |
---|
comments powered by Disqus