MCPcopy Create free account
hub / github.com/neetcode-gh/leetcode / shiftGrid

Method shiftGrid

python/1260-shift-2d-grid.py:2–16  ·  view source on GitHub ↗
(self, grid: List[List[int]], k: int)

Source from the content-addressed store, hash-verified

1class 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

Callers

nothing calls this directly

Calls 2

posToValFunction · 0.85
valToPosFunction · 0.85

Tested by

no test coverage detected