0

I am doing some stuff on leetcode and came up with solution it works fine but some cases. Here is the problem itself:

enter image description here

But in case like this it doesn't:

enter image description here

It doesn't make sense how can I rotate elements if k is bigger than length of array. If you have any idea how to improve this solution I would be grateful

class Solution:
    def rotate(self, nums: List[int], k: int) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        if len(nums) > k:
            self.swap(nums, 0, len(nums)-1)
            self.swap(nums, 0,k-1)
            self.swap(nums, k, len(nums)-1)


    def swap(self, nums, start, end):

        while start < end:
            nums[start], nums[end] = nums[end], nums[start]
            start+=1
            end-=1
norok2
  • 25,683
  • 4
  • 73
  • 99
Ulukbek Abylbekov
  • 449
  • 1
  • 6
  • 19
  • You should check out the code review stack exchange – Thomas Ahle Jul 18 '19 at 00:40
  • 3
    the modulo operator, `%` will be useful. For example, if an array is 5 long, and you want to rotate by 5, you end up with the same array. So technically, you'd optimally want to rotate by 0. This is where the `%` operator comes into play. `5 % 5 = 0`. If we want to rotate an array length 5 by 7 spots, we would end up with the same thing as rotating the array by 2, and it turns out that `7 % 5 = 2`. Do you see where I am going with this? – JamieT Jul 18 '19 at 00:46
  • Why don't you just output elements from k-th to n'th, and then from first to k-th? If k > array length then use modulo as described above – Maras Jul 18 '19 at 17:22

2 Answers2

2

In order to understand why this doesn't work for the cases where k is larger than the array length, let me try to explain some of the logic behind rotating by such values of k.

The modulo operator, % will be useful. For example, if an array is 5 long, and you want to rotate by 5, you end up with the same array. So technically, you'd optimally want to rotate by 0. This is where the % operator comes into play. 5 % 5 = 0. If we want to rotate an array length 5 by 7 spots, we would end up with the same thing as rotating the array by 2, and it turns out that 7 % 5 = 2. Do you see where I am going with this?

This also holds true if the value of k is less than the length of the array. Say we want to rotate an array length 5 by 3, we do 3 % 5 = 3.

So for any rotation of amount k and array length L, optimization rotation amount n is equivalent to n = k % L.

You should modify your code at the beginning of your rotate method to adjust the rotation amount:

k = k % L

and use this value to rotate the correct amount.

JamieT
  • 1,177
  • 1
  • 9
  • 19
0

The fastest and cleanest solution by far and large is:

def rotate_right(items, shift):
    shift = -shift % len(items)
    return items[shift:] + items[:shift]


ll = [i + 1 for i in range(7)]
# [1, 2, 3, 4, 5, 6, 7]
rotate_right(ll, 3)
# [5, 6, 7, 1, 2, 3, 4]

rotate_right([1, 2], 3)
# [2, 1]

of course, short of using numpy.roll() or itertools.cycle().

norok2
  • 25,683
  • 4
  • 73
  • 99