Show problems as
Classic problem #1260EasyArrayMatrixSimulation

Shift 2D Grid

Real-world version: Rolling the Tile Board Forward
Tracked in this browser only
Read Shift 2D Grid on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A tile board is a grid of m rows by n columns of numbers, given as grid.

One roll moves every tile one place along, reading the board row by row from the top left: a tile moves to the next column of its own row, a tile in the last column moves to the first column of the row below, and the tile in the bottom right corner moves to the top left.

Return the board after k rolls.

Examples

Example 1

Input
grid = [[4, 7, 2], [8, 5, 9], [3, 6, 1]], k = 1
Output
[[1, 4, 7], [2, 8, 5], [9, 3, 6]]

Every tile moves one place along the row-by-row reading, and the 1 in the bottom right corner wraps round to the top left.

Example 2

Input
grid = [[4, 7, 2], [8, 5, 9], [3, 6, 1]], k = 9
Output
[[4, 7, 2], [8, 5, 9], [3, 6, 1]]

The board holds nine tiles, so nine rolls bring every tile back to where it started.

Example 3

Input
grid = [[6], [8]], k = 1
Output
[[8], [6]]

The board has a single column, so the 6 moves down into the second row and the 8 wraps round to the first.

Constraints

  • 1 <= grid.length <= 50
  • 1 <= grid[0].length <= 50
  • Every row of grid has the same length
  • -1000 <= grid[i][j] <= 1000
  • 0 <= k <= 100

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def roll_board(grid: list[list[int]], k: int) -> list[list[int]]:
Java
public List<List<Integer>> rollBoard(int[][] grid, int k)