Rotate Array - Leetcode 189 - Python

preview_player
Показать описание


0:00 - Read the problem
1:35 - Drawing Explanation
6:33 - Coding Explanation

leetcode 189

#rotate #array #python
Disclosure: Some of the links above may be affiliate links, from which I may earn a small commission.
Рекомендации по теме
Комментарии
Автор

def rotate(nums, k):
def rev(l, r):
while l < r:
nums[l], nums[r] = nums[r], nums[l]
l +=1
r -=1

rev(0, len(nums) - 1)
rev(0, k-1)
rev(k, len(nums)-1)
return nums

# saving you all to write while loop three times

Pakoda-hcej
Автор

Thanks you, I was stuck trying to do the O(1) solution. Great explanation!

liegon
Автор

What a great solution and explanation! I have nothing to say but "thank you, man”!

happyhacker
Автор

I got stuck at 37/38 cases and I hit time limit exceeded. I was trying to do O(1) space. Your approach was so different than mine 😂 this one had me stumped

davidm
Автор

Thanks for the explanation :)

Also appreciate the chapters in the vid! Makes it super easy to navigate

sidthakur
Автор

Great explanation again :) Though I can't keep myself not thinking, how could I think this and code within 30 minutes?

MsSkip
Автор

This is the best I found, very clear explanation, thank you!

fredlala
Автор

My bruteforce approach:


class Solution:
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
for i in range(k):
l = nums[len(nums)-1]
nums.pop()
nums.insert(0, l)

Mukeshsainaidu
Автор

I think this solution is even easier
k = k % len(nums)
if k != 0:
nums[:] = nums[-k:] + nums[:len(nums)-k]

You can just slice last k elem and first len(nums)-k elems
It's also O(n) in time but takes O(n) space, although leet code shows the same amount of space for both (maybe because of caching?)

bartekbinda
Автор

Cannot thank enough for all your videos!

RanjuRao
Автор

Updated versrion with helper function >> rev()
```
class Solution:
def rotate(self, nums: List[int], k: int) -> None:
k = k % len(nums)

def rev(l=0, r=len(nums) - 1):
while l < r:
nums[l], nums[r] = nums[r], nums[l]
l, r = l + 1, r -1
rev()
rev(r=k-1)
rev(l=k)
```

yusufadel
Автор

Thank you, sir; I learned a lot from your tutorials.

chandravijaya
Автор

There is a second way to do it. Let's say you have 1, 2, 3, 4, 5. and k = 2. Step 1. move index 0 number to k+0 position(2). the array now will be _, 2, 1, 4, 5 and you use a temp int to store the index k+i number which is 3. Step 2.instead of moving index 1 number, you move k+2 number. u shift 3 to 3+k position. now you temp int will be 5. and array will be _, 2, 1, 4, 3. With 3 more steps you will get your result. You are only using 1 temp integer so the space is O(1) and the time is O(n)

yazoncui
Автор

Pop and insert using a for loop. Done😌

dnyanesh.s
Автор

in the bound condition : k=k%len(nums), you said if k is greater than the len(nums) then this condition to be applied but since we haven't made that condition prior so how is k not changing for cases where k is smaller than the len(nums); can somebody explain me this, thankyou!

hemantsikarwar
Автор

swift version -
func rotate(_ nums: inout [Int], _ k: Int) {
var nItems = nums.count
var k = k % nItems
var targetSlice = nums[nItems - k..<nItems] + nums[0..<nItems - k]
nums = Array(targetSlice)


}

gihan
Автор

for space O(n), the shifting formula is, ans[i] = nums[(i + size - k)%size];

KunalVerma-gipq
Автор

Came up with another soln this too is a constant space soln and works for rotating the array to the left too for some problems (just in case) by slight changes

code -

public static int[] nums, int k){
int n = nums.length;
k=k%n;
int[] temp = new int[k];
int m=0;
for(int i=n-k;i<n;i++){
temp[m] = nums[i];
m++;
}

for(int i=n-1;i>=k;i--){
nums[i] = nums[i-k];
}

int j=0;
for (int i = 0; i < k; i++) {
nums[i] = temp[i];
}
return nums;
}

k=k%n was wild throwing a runtime exception lol

priyamf
Автор

Nice explaination as always!
Requesting you to please solve cherry pickup, stone game dp solution or bitmasking dp questions.

amogchandrashekar
Автор

i did this.
class Solution:
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
while k >0:
ele = nums.pop()
nums.insert(0, ele)
k -= 1

thamaraiselvan