Skip to content

Commit d0d89f7

Browse files
authored
Merge pull request neetcode-gh#1746 from AP-Repositories/patch-29
Create 1260-shift-2d-grid.py
2 parents 6aa86e4 + 59469a9 commit d0d89f7

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

python/1260-shift-2d-grid.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def shiftGrid(self, grid: List[List[int]], k: int) -> List[List[int]]:
3+
M, N = len(grid), len(grid[0])
4+
5+
def posToVal(r, c):
6+
return r * N + c
7+
def valToPos(v):
8+
return [v // N, v % N] # r, c
9+
10+
res = [[0] * N for i in range(M)]
11+
for r in range(M):
12+
for c in range(N):
13+
newVal = (posToVal(r, c) + k) % (M * N)
14+
newR, newC = valToPos(newVal)
15+
res[newR][newC] = grid[r][c]
16+
return res

0 commit comments

Comments
 (0)