DEV Community

So Sun Park
So Sun Park

Posted on

Day 3. Two pointers - rotate array

While the topic of this problem is clearly stated as two pointers, as I got obsessed with the question itself, I forgot the core idea or approaches. So I had to refer to some of the posts to realize how I can use two pointers to approach this problem.

189. Rotate Array

Given an integer array nums, rotate the array to the right by k steps, where k is non-negative.

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]
Enter fullscreen mode Exit fullscreen mode

Learning points

  • I get how reverse all, reverse first part, reverse the other part approach works the best. But still in the process of understanding how to come up with an idea from the first place hmm...
  • For all 3 types of solutions, a remainder operation for k is the key to solve the cases in which k > nums.length.
if(k > nums.length) {
  k = k % nums.length;
}
Enter fullscreen mode Exit fullscreen mode

Initial attempts

  • Without k%nums.length operation, the solution 2 didn't work for nums=[1,2], k = 5 test case.
  • solution 1 works fine, but when I submitted, it showed time limit exceeded - even with k%nums.length operation.

Solution 1

    if(k > nums.length) {
        k = k % nums.length;
    }
    for(let i = 0; i < k; i++) {
        // pop from last index
        let last = nums.pop();
        // insert to first index
        nums.unshift(last);
    }
Enter fullscreen mode Exit fullscreen mode

Solution 2

    if(k > nums.length) {
        k = k % nums.length;
    }
    let k_nums = nums.slice(nums.length - k, nums.length); 
    // slice: returns new array of start ~ end-1 
    // splice: delete original array from start ~ end-1

    // O(n)
    for(let i = k_nums.length - 1; i >= 0; i--) {
        let n = k_nums[i];
        nums.unshift(n);
    }

Enter fullscreen mode Exit fullscreen mode
  • For solution 2, I first attempted to slide (make a new array of last k items - [4, 5, 6] ), splice (delete k items from the original - [0, 1, 2, 3] ) and then concat. Then I tried to do nums = a result of concat [4, 5, 6, 0, 1, 2, 3].
  • But because the leetcode question was asking to modify the nums - the original array, it didn't work.
  • So I had to find another way to modify the nums. Here, I used unshift with for loop to add [4,5,6] in backwards order to the beginning of spliced array [0, 1, 2, 3].

Final code

var reverse = function(nums, lower, upper) {
    while(lower < upper) {
        let temp = nums[lower];
        nums[lower] = nums[upper];
        nums[upper] = temp;

        lower++;
        upper--;
    }
}
var rotate = function(nums, k) {
    // solution 3: FINAL
    // reverse the entire array
    reverse(nums, 0, nums.length - 1);
    if(k > nums.length) {
        k = k % nums.length;
    }
    // reverse first half array
    reverse(nums, 0, k - 1);
    // reverse last half array
    reverse(nums, k, nums.length - 1);
};
Enter fullscreen mode Exit fullscreen mode

Helpful posts

relevant or similar quizzes

Related problems / quizzes

Top comments (0)