LeetCode 1260 Shift 2D Grid

LeetCode 1260 Shift 2D Grid Problem

Download Code
class Solution(object):
    def shiftGrid(self, grid, k):
        :type grid: List[List[int]]
        :type k: int
        :rtype: List[List[int]]
        # m * n temp array
        new_grid = [[0] * len(grid[0]) for _ in range(len(grid))]
        m = len(grid)
        n = len(grid[0])
        # Compute final location
        true_k = k % (m * n)
        # row move
        move_i = true_k / n
        # col move
        move_j = true_k % n

        for i in range(m):
            for j in range(n):
                new_i = i + move_i
                # move one row if move_j + j >= n
                if move_j + j >= n:
                    new_i += 1
                new_i %= m
                new_j = (j + move_j) % n
                new_grid[new_i][new_j] = grid[i][j]
        return new_grid
Download Shift 2D Grid.py

List of all Shift 2D Grid problems

Leetcode 1260 Shift 2D Grid problem solution in python3 with explanation. This is the best place to expand your knowledge and get prepared for your next interview.

Feedback is the most important part of any website.

If you have any query, suggestion or feedback, Please feel free to contact us.