Leetcode 1386 Solution

This article provides solution to leetcode question 1386 (shift-2d-grid)

https://leetcode.com/problems/shift-2d-grid

Solution

class Solution:
    def shiftGrid(self, grid: List[List[int]], k: int) -> List[List[int]]:
        m = len(grid)
        n = len(grid[0])

        ans = [[0 for _ in range(n)] for _ in range(m)]

        for i in range(m):
            for j in range(n):
                index = ((i * n + j) + k) % (m * n)

                r = index // n
                s = index % n
                ans[r][s] = grid[i][j]

        return ans