Shift 2D Grid - Leetcode 1260 - Python

  Рет қаралды 20,130

NeetCode

NeetCode

Күн бұрын

🚀 neetcode.io/ - A better way to prepare for Coding Interviews
🥷 Discord: / discord
🐦 Twitter: / neetcode1
🐮 Support the channel: / neetcode
⭐ BLIND-75 PLAYLIST: • Two Sum - Leetcode 1 -...
💡 DYNAMIC PROGRAMMING PLAYLIST: • House Robber - Leetco...
Problem Link: leetcode.com/problems/shift-2...
0:00 - Read the problem
1:40 - Drawing Explanation
6:37 - Coding Explanation
leetcode 1260
#facebook #interview #python
Disclosure: Some of the links above may be affiliate links, from which I may earn a small commission.

Пікірлер: 52
@GunnerJayx
@GunnerJayx 2 жыл бұрын
Wow I was just trying this LeetCode question of the day wishing there was a Neetcode video for it...and here we are
@sivaganesh4489
@sivaganesh4489 2 жыл бұрын
same here
@udattam
@udattam 2 жыл бұрын
Loved your solution. I have previously seen problems where we convert a r,c to a single value but never got the intuition behind it. But it's all clear now. Thanks again for the clear explanation!
@avedissimracing9628
@avedissimracing9628 2 жыл бұрын
5:33 why do we mod by 2? We have to mod by 3 - the number of columns
@NeetCode
@NeetCode 2 жыл бұрын
Yeah that's correct
@studyaccount794
@studyaccount794 2 жыл бұрын
Thanks, I was stuck in this problem. This is a tricky one for an easy problem I think.
@NeetCode
@NeetCode 2 жыл бұрын
Yeah I agree!
@sivaganesh4489
@sivaganesh4489 2 жыл бұрын
Thanks for great explanation. expecting a gird series from you
@hamdysaadpersonal
@hamdysaadpersonal 2 жыл бұрын
love it and love the way you solve it
@shantanukumar4081
@shantanukumar4081 2 жыл бұрын
Great Explanation !!!
@AKrieger94
@AKrieger94 2 жыл бұрын
Wow! just in time :D I was literally gonna search for this problem in youtube, but it appeared in my youtube suggestions
@NotKevinNguyen
@NotKevinNguyen 2 жыл бұрын
I was looking for your video earlier today haha
@nehaa3778
@nehaa3778 2 жыл бұрын
Clear and helpful!
@Marcelo-yp9uz
@Marcelo-yp9uz 2 жыл бұрын
great explanation!
@andrewgleason7588
@andrewgleason7588 2 жыл бұрын
great use of helper functions, make it much more readable of a solution
@spacex6997
@spacex6997 Жыл бұрын
Would it be possible to convert the 2D list into 1D, do the scrolling and then for each 3 elements, add those into 3 lists for 9 items?
@Raymond-Wu
@Raymond-Wu 2 жыл бұрын
Glad we had the same approach! I immediately decided to flatten the arr when I saw this problem. Only difference is that I simulated the k shifting with popping and inserting at index 0. That way I didn't have to think about when things would go out of bounds
@sprinklepancake
@sprinklepancake 2 ай бұрын
thanks man
@abheykalia3409
@abheykalia3409 2 жыл бұрын
(k/n)%m will tell us how many places do we need to shift the complete rows by. and do the k%n number of rotations on this new matrix. I did in this way
@sarveshmaurya2168
@sarveshmaurya2168 2 жыл бұрын
well explained
@impostorX
@impostorX 2 жыл бұрын
Thanks! But can we do it in place for O(1) space complexity?
@hantinglu8050
@hantinglu8050 2 жыл бұрын
Check out the concept of 189. Rotate Array. Basically they're the same instead of this is a 2d problem.
@genuinepriyanshu8685
@genuinepriyanshu8685 3 ай бұрын
We can use inplace shifting using one temporary variable so that SC will be O(1)
@shenyan9062
@shenyan9062 2 жыл бұрын
nice solution
@Faha_Kite
@Faha_Kite 2 жыл бұрын
Nice explanation. Can you share how you make your videos?
@pranavsharma7479
@pranavsharma7479 2 жыл бұрын
this shd be a medium problem it requires this 2d to 1d and 1d to 2d trick + shift circularly
@vaibhavvashist238
@vaibhavvashist238 2 жыл бұрын
Hi How much time you spend in particular problem without looking for solution?
@NeetCode
@NeetCode 2 жыл бұрын
I think 15-20min is a good rule of thumb.
@timarleyfoster5417
@timarleyfoster5417 Жыл бұрын
So is N the number of columns or values in a row, because he goes back to saying 'values in a row' in the code part?
@linhnguyenduc641
@linhnguyenduc641 6 ай бұрын
To recap: 1.Treat 2D array as 1D one 2.Shift items in 1D array because it is easy to find a new position after shifting 3.Convert a new position back to a new row and a new col in 2D array 4.Update value of new row and new col in result
@artemis5507
@artemis5507 2 жыл бұрын
Flatten-> then used deque->again unflatten... faster but man to much space consumption 😮‍💨
@lordsixth5944
@lordsixth5944 2 жыл бұрын
Damm never thought about this one. Tho how old are you?
@Od253
@Od253 2 жыл бұрын
This easy is lowkey difficult
@NeetCode
@NeetCode 2 жыл бұрын
Agreed
@jerryjohnthomas4908
@jerryjohnthomas4908 2 жыл бұрын
Hi absoltely love your channel. Could you pleaseeeee do 979. Distribute Coins in Binary Tree.(or 968. Binary Tree Cameras ) i know its a lot to ask but you are awesome :')
@ritikgoyal3622
@ritikgoyal3622 2 жыл бұрын
From where I can learn a python oops pls suggest me Any sources
@jugsma6676
@jugsma6676 10 ай бұрын
I was putting all row and cols in 1D and back to grid cache = [] for i in range(len(grid)): for j in range(len(grid[i])): cache.append(grid[i][j]) tmp1 = cache[:-k] tmp2 = cache[-k:] cache = tmp2 + tmp1 p = 0 for i in range(len(grid)): for j in range(len(grid[i])): grid[i][j] = cache[p] p += 1 return grid
@Marcelo-yp9uz
@Marcelo-yp9uz 2 жыл бұрын
5:50 Seven divided by three is also two*
@cozywind2010
@cozywind2010 2 жыл бұрын
I found this solution to be more intuitive and cleaner. the sum(grid,[]) is new to me though. class Solution: def shiftGrid(self, grid: List[List[int]], k: int) -> List[List[int]]: col=len(grid[0]) nums=sum(grid,[]) k = k % len(nums) move=len(nums)-k ans=grid count=0 nums = nums[move:] + nums[:move] for i in range(0,len(nums),col): ans[count]=nums[i:i+col] count+=1 return ans
@numberonep5404
@numberonep5404 2 жыл бұрын
fun pb
@smokesmoker4301
@smokesmoker4301 2 жыл бұрын
use c and use pointers.
@BeLikeNexus
@BeLikeNexus 2 жыл бұрын
I feel like this is not an easy lol
@NeetCode
@NeetCode 2 жыл бұрын
Yeah agree
@bhaveshverma8629
@bhaveshverma8629 2 жыл бұрын
Still not understand
@dataman4503
@dataman4503 2 жыл бұрын
C++. vector shiftGrid(vector& grid, int k) { int rows = grid.size(); int cols = grid[0].size(); // -- Optimization -- (Edge case) // After rows*cols shifts, the result grid will be same as original input grid. if (k % (rows*cols) == 0) return grid; vector res(rows, vector(cols, 0)); for (int r=0; r
@imohanreddy
@imohanreddy 2 жыл бұрын
can someone help why this is not working ? Please class Solution: def shiftGrid(self, grid: List[List[int]], k: int) -> List[List[int]]: m, n = len(grid), len(grid[0]) ans = [[0]*n for i in range(m)] for r in range(m): for c in range(n): one=((r*n +c)+k)%(m*n) print(one) j,k=one//n, one%n ans[j][k]=grid[r][c] return ans
@imohanreddy
@imohanreddy 2 жыл бұрын
tried without functionand "one" doesn't seem to be calculating right , not able to figure it out
@NeetCode
@NeetCode 2 жыл бұрын
Your issue is that 'k' is an input parameter, but your are reassigning it in your for loop.
@imohanreddy
@imohanreddy 2 жыл бұрын
@@NeetCode oh yes, thanks a lot
@RajanPaudel-xh7xv
@RajanPaudel-xh7xv Ай бұрын
hard
@pphodaie
@pphodaie 2 жыл бұрын
I flattened the grid to an array, removed the last k % m * n elements of this array and added it to the start of the array. Finally I converted the shifted array back to a grid. func shiftGrid(_ grid: [[Int]], _ k: Int) -> [[Int]] { let m = grid.count let n = grid[0].count let array = grid.flatMap{$0} let size = array.count let tk = k % array.count let shifitedArray = Array(array[(size - tk)...] + array[..
Ugly Number - Leetcode 263 - Python
10:44
NeetCode
Рет қаралды 23 М.
Find Eventual Safe States - Leetcode 802 - Python
13:18
NeetCode
Рет қаралды 24 М.
Clown takes blame for missing candy 🍬🤣 #shorts
00:49
Yoeslan
Рет қаралды 38 МЛН
Now THIS is entertainment! 🤣
00:59
America's Got Talent
Рет қаралды 38 МЛН
Sigma Kid Hair #funny #sigma #comedy
00:33
CRAZY GREAPA
Рет қаралды 32 МЛН
The Python Function You NEED For 2D Data
10:49
Mr. P Solver
Рет қаралды 36 М.
System Design Interview - Rate Limiting (local and distributed)
34:36
System Design Interview
Рет қаралды 289 М.
Maximum Frequency Stack - Leetcode 895 - Python
13:21
NeetCode
Рет қаралды 26 М.
The Java Memory Model - The Basics
23:41
Jakob Jenkov
Рет қаралды 122 М.
N-Queens - Backtracking - Leetcode 51 - Python
17:51
NeetCode
Рет қаралды 154 М.
Range Sum Query 2D - Immutable - Leetcode 304 - Python
13:17
NeetCode
Рет қаралды 36 М.
Valid Anagram - Leetcode 242 - Python
12:01
NeetCode
Рет қаралды 478 М.
All Rust string types explained
22:13
Let's Get Rusty
Рет қаралды 155 М.
iPhone, Galaxy или Pixel? 😎
0:16
serg1us
Рет қаралды 944 М.
Samsung Galaxy 🔥 #shorts  #trending #youtubeshorts  #shortvideo ujjawal4u
0:10
Ujjawal4u. 120k Views . 4 hours ago
Рет қаралды 8 МЛН