Shift 2D Grid

Easy Topics Company Tags

Shift the rectangular matrix grid to the right k times and return the resulting matrix. During each shift, every value moves one column to the right. A value leaving the last column moves to the first column of the next row; the bottom-right value wraps around to the top-left cell. All moves within one shift happen simultaneously.

Example 1:

Shift 2D Grid, example 1: illustration of the example

Input: grid = [[1,2],[3,4]]
k = 1

Output: [[4,1],[2,3]]

Explanation: The values 1 and 3 move right, 2 moves to the start of the next row, and 4 wraps to the top-left corner. The resulting rows are [4, 1] and [2, 3].

Example 2:

Shift 2D Grid, example 2: illustration of the example

Input: grid = [[5,6,7]]
k = 2

Output: [[6,7,5]]

Explanation: After one shift the row is [7, 5, 6]. A second shift produces [6, 7, 5].

Example 3:

Shift 2D Grid, example 3: illustration of the example

Input: grid = [[8],[9]]
k = 0

Output: [[8],[9]]

Explanation: Zero shifts leave both cells in their original positions, so the matrix is unchanged.

Constraints:

  • grid is rectangular, with between 1 and 50 rows and between 1 and 50 columns.
  • -1000 <= grid[i][j] <= 1000
  • 0 <= k <= 100


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.