Skip to content

Commit 59469a9

Browse files
Create 1260-shift-2d-grid.py
This code is transcribed from the video https://www.youtube.com/watch?v=nJYFh4Dl-as
1 parent 2d0e0cc commit 59469a9

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)