Skip to content

Commit

Permalink
Create 1260-shift-2d-grid.py
Browse files Browse the repository at this point in the history
This code is transcribed from the video https://www.youtube.com/watch?v=nJYFh4Dl-as
  • Loading branch information
AP-Repositories authored Dec 30, 2022
1 parent 2d0e0cc commit 59469a9
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions python/1260-shift-2d-grid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution:
def shiftGrid(self, grid: List[List[int]], k: int) -> List[List[int]]:
M, N = len(grid), len(grid[0])

def posToVal(r, c):
return r * N + c
def valToPos(v):
return [v // N, v % N] # r, c

res = [[0] * N for i in range(M)]
for r in range(M):
for c in range(N):
newVal = (posToVal(r, c) + k) % (M * N)
newR, newC = valToPos(newVal)
res[newR][newC] = grid[r][c]
return res

0 comments on commit 59469a9

Please sign in to comment.