Cyclically Rotating a Grid

Tags : array, matrix, leetcode, cpp, medium

You are given an m x n integer matrix grid​​​, where m and n are both even integers, and an integer k.

The matrix is composed of several layers, which is shown in the below image, where each color is its own layer:

A cyclic rotation of the matrix is done by cyclically rotating each layer in the matrix. To cyclically rotate a layer once, each element in the layer will take the place of the adjacent element in the counter-clockwise direction. An example rotation is shown below:


Return the matrix after applying k cyclic rotations to it.

Examples #

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.

Constraints #

Solutions #

class Solution {
public:
    vector<vector<int>> rotateGrid(vector<vector<int>>& grid, int k) {
        int n = grid.size();
        int m = grid[0].size();
        int l=0,u=0,r=m-1,d=n-1;
        int level = min(m,n) / 2;
        while(level--){
            vector<int> temp;
            for(int i=l; i<=r; i++)
                temp.push_back(grid[u][i]);
            for(int i=u+1; i<=d; i++)
                temp.push_back(grid[i][r]);
            for(int i=r-1; i>=l; i--)
                temp.push_back(grid[d][i]);
            for(int i=d-1; i>u; i--)
                temp.push_back(grid[i][l]);
            
            int tsz = temp.size();
            int ti=0;
            for(int i=l; i<=r; i++,ti++)
                grid[u][i] = temp[(k+ti)%tsz];
            for(int i=u+1; i<=d; i++,ti++)
                grid[i][r] = temp[(k+ti)%tsz];
            for(int i=r-1; i>=l; i--,ti++)
                grid[d][i] = temp[(k+ti)%tsz];
            for(int i=d-1; i>u; i--,ti++)
                grid[i][l] = temp[(k+ti)%tsz];
            
            l++; r--;
            u++; d--;
        }
        
        return grid;
    }
};