#1914

Cyclically Rotating a Grid

specialist · 905 · lc medium +31 · premium · verified · 51.6% accepted · 271 likes · top 41%

Description

You are given an m x n integer matrix grid (both dimensions even) and an integer k. The matrix has concentric ring layers. One cyclic rotation moves every element of a layer one position counter-clockwise.

Return the matrix after exactly k such cyclic rotations.

Example 1:

Input: grid = [[40,10],[30,20]], k = 1
Output: [[10,20],[40,30]]
Explanation: The figures above represent the grid at every state.

Example 2:

Input: grid = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]], k = 2
Output: [[3,4,8,12],[2,11,10,16],[1,7,6,15],[5,9,13,14]]
Explanation: The figures above represent the grid at every state.

Code

1
2