0

I've solved one of Leetcode problems in codepen, but when I try to submit it on leetcode I get a different result than what I get in codepen.

The problem is:

Given an array, 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]

Link (requires login) https://leetcode.com/explore/interview/card/top-interview-questions-easy/92/array/646/

My solution:

var rotate = function(nums, k) {
  var a = nums.splice(0 , nums.length-k);
  var b = nums.splice(-k);
  var c = [...b , ...a ];
  return(c);
};

Using the above example, running this code on codepen returns (or console.logs) [5,6,7,1,2,3,4].

But when I run this code in leetcode, I get an empty array [].

Any ideas why this could be the happening?

Baslki
  • 63
  • 1
  • 1
  • 9
  • Read carefully: `@return {void} Do not return anything, modify nums in-place instead.` – Rango Dec 22 '22 at 17:11
  • Also `.slice` and `.splice` are completely different. – Rango Dec 22 '22 at 17:12
  • Using splice mutates the array, using slice doesn't. Take a look at which one you need as the first `nums.splice` is changing the content of your original array. Take a look at the docs here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice – Daniel Dec 22 '22 at 17:13
  • Hint: you need exactly one `.splice` to solve the problem ;) – Rango Dec 22 '22 at 17:15

2 Answers2

1

It looks like you're supposed to rotate the original array, not return a new rotated array. So you need to set nums = [...b, ...a] instead.

EDIT: Since JavaScript passes by value, the nums parameter just holds a reference to the original array, so doing nums = [] will only change the nums variable to reference a different array, without changing the original array. You'll want to call methods of the original array to mutate it. E.g. .splice

/EDIT

Also, have you thought about what happens when k is greater than the length of the array? E.g. nums = [1, 2, 3], k = 5

Also also, your link to leetcode requires login, so not everyone will be able to view it.

LawrenceWebDev
  • 439
  • 2
  • 7
1

Example A

.pop() removes the last element of an array and returns it. .unshift() the returned value of .pop() to the first index of the array. Do that k times in a for loop. .pop() and .unshift() changes (aka mutates) the original array.

let arr = [1, 2, 3, 4, 5, 6, 7], k = 3;

function rotate(array, times) {
  for (let i=0; i < times; i++) {
    let last = array.pop();
    array.unshift(last);
  }
  return array;
}

console.log(rotate(arr, k));
  

Example B

By changing .splice() to .slice() you can implement your logic since .slice() creates a shallow copy of the array and does not mutate the original array like .splice(). Since the original array is unchanged, any references to said array are consistent. Also, concerning the case mentioned in LawrenceWebDev's answer -- if k is greater than the length of the array -- k (times) becomes the remainder of k/array.length (times % size).

let arr = [1, 2, 3, 4, 5, 6, 7], k = 3;

function rotate(array, times) {
  const size = array.length;
  if (times > size) {
    times = times % size;
  }
  let a = array.slice(-times);
  let b = array.slice(0, size - times);
  return [...a, ...b];
}

console.log(rotate(arr, k));
zer00ne
  • 41,936
  • 6
  • 41
  • 68
  • This is an O(N^2) algorithm because `unshift()` is O(N), so it probably will get a TLE at LeetCode. – Barmar Dec 22 '22 at 17:49
  • @Barmar yes, this does give me the correct output when I "run code" on leetcode, but I get a TLE error when I "submit" the solution. – Baslki Dec 22 '22 at 17:55
  • O(N) is how I think -- methodically slow – zer00ne Dec 22 '22 at 17:56
  • @Baslki just updated answer, try **Example B** which goes along with the OP logic. – zer00ne Dec 22 '22 at 18:42
  • @zer00ne I appreciate your time, however Example B returns [5,6,7,1,2,3,4] in CodePen, but return [1,2,3,4,5,6,7] in Leetcode. – Baslki Dec 22 '22 at 21:09
  • Sorry, I'm not familiar with Leetcode , BTW what is a TLE? – zer00ne Dec 22 '22 at 21:18
  • @zer00ne it's "Time Limit Exceeded". BTW I tried one of the official solutions posted by leetcode, and I got the TLE. I think it's an issue with leetcode. – Baslki Dec 22 '22 at 23:43