Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1746 from AP-Repositories/patch-29
Browse files Browse the repository at this point in the history
Create 1260-shift-2d-grid.py
  • Loading branch information
Ahmad-A0 authored Jan 1, 2023
2 parents 6aa86e4 + 59469a9 commit d0d89f7
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 d0d89f7

Please sign in to comment.