All problems
0846EasyArrayMatrixSimulation

Rolling the Tile Board Forward

Tracked in this browser only
Write code

Trains the technique from

LeetCode 1260Shift 2D Grid

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a 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)
September 7
Apply