Shift 2D Grid
Real-world version: Rolling the Tile Board ForwardThe 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.
def roll_board(grid: list[list[int]], k: int) -> list[list[int]]:public List<List<Integer>> rollBoard(int[][] grid, int k)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 14 test cases: 14 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Reshape the MatrixReal-world version: Relaying the Contact SheetEasy
- Transpose MatrixReal-world version: Flip a Reading Grid Onto Its SideEasy
- Find Winner on a Tic Tac Toe GameReal-world version: Who Comes Out Ahead on the Three-by-ThreeEasy
- Convert 1D Array Into 2D ArrayReal-world version: Laying the Tiles Into a RackEasy