Ctrl K

Rolling Grid

Easy
You are given a grid grid with m rows and n columns and an integer steps. Imagine reading the cells row by row, like text, and closing them into a ring: after the last cell of a row comes the first cell of the next row, and after the very last cell of the grid comes the very first one. In one step every number moves to the next cell of that ring: - from grid[i][j] to grid[i][j + 1] if it is not the last column; - from grid[i][n - 1] to grid[i + 1][0]; - from grid[m - 1][n - 1] to grid[0][0]. Return the grid after steps steps.
Example 1
grid=[[1, 2], [3, 4], [5, 6]], steps=1 → [[6, 1], [2, 3], [4, 5]]

Every number moves one cell right, 2 and 4 wrap to the start of the next row, and 6 from the last cell goes to the very first one.

Example 2
grid=[[1, 2, 3], [4, 5, 6]], steps=3 → [[4, 5, 6], [1, 2, 3]]

Three steps in a grid three cells wide move everything exactly one row down, so the rows trade places.

Example 3
grid=[[7, -7]], steps=4 → [[7, -7]]

The grid has two cells, so every two steps everything is back in place; after four steps the grid is unchanged.

Constraints
  • 1 ≤ m, n ≤ 50
  • -1000 ≤ grid[i][j] ≤ 1000
  • 0 ≤ steps ≤ 100
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsRepack the Tray EasyMirror and Negate a Bitmap EasySwap Rows and Columns EasyPawns the Rook Can Take EasyNoughts and Crosses Verdict Easy
loading Python…
Input
grid=[[1, 2], [3, 4], [5, 6]], steps=1
Expected
[[6, 1], [2, 3], [4, 5]]

Run checks the first 3 tests in your browser. Submit checks all 83 tests on the server, hidden ones included.