1144. Staircase Shear

Easy1.5 points estimatedMatrixArraySimulation

A pixel art tool applies a staircase shear to an image stored as a grid of integers. Row r (0-based) is rotated cyclically to the right by r mod n positions, where n is the number of columns. Row 0 stays as it is, row 1 moves one step to the right with its last value wrapping around to the front, row 2 moves two steps, and so on; when r reaches n the rotation wraps back to zero steps. Every row is rotated on its own, and the values inside a row keep their order apart from the rotation. Modify grid in place and return nothing.

The grid has m rows and n columns with 1 <= m, n <= 50 and every value between -10^4 and 10^4. O(m * n) time and O(n) extra space is intended.

Example 1

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

Row 1 shifts right by one and row 2 by two, giving [[1,2,3],[6,4,5],[8,9,7]].

Example 2

Input:
grid = [[1,2],[3,4],[5,6]]
Output:
[[1,2],[4,3],[5,6]]
Explanation:

Row 1 shifts one place right to [4, 3], and row 2 shifts by 2 mod 2 = 0 places, so it stays unchanged.

Constraints

1 ≤ m, n ≤ 50

-104 ≤ grid[i][j] ≤ 104

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Graded
Your answer is read from grid after your method returns.

Expected complexity

Time
O(m * n)
Space
O(n)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…