1914. Cyclically Rotating a Grid LeetCode Solution

In this guide, you will get 1914. Cyclically Rotating a Grid LeetCode Solution with the best time and space complexity. The solution to Cyclically Rotating a Grid problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Cyclically Rotating a Grid solution in C++
  4. Cyclically Rotating a Grid solution in Java
  5. Cyclically Rotating a Grid solution in Python
  6. Additional Resources
1914. Cyclically Rotating a Grid LeetCode Solution image

Problem Statement of Cyclically Rotating a Grid

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.

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:

m == grid.length
n == grid[i].length
2 <= m, n <= 50
Both m and n are even integers.
1 <= grid[i][j] <= 5000
1 <= k <= 109

Complexity Analysis

  • Time Complexity: O(mnk)
  • Space Complexity: O(mn)
See also  1879. Minimum XOR Sum of Two Arrays LeetCode Solution

1914. Cyclically Rotating a Grid LeetCode Solution in C++

class Solution {
 public:
  vector<vector<int>> rotateGrid(vector<vector<int>>& grid, int k) {
    const int m = grid.size();
    const int n = grid[0].size();
    int t = 0;      // the top
    int l = 0;      // the left
    int b = m - 1;  // the bottom
    int r = n - 1;  // the right

    while (t < b && l < r) {
      const int elementInThisLayer = 2 * (b - t + 1) + 2 * (r - l + 1) - 4;
      const int netRotations = k % elementInThisLayer;
      for (int rotate = 0; rotate < netRotations; ++rotate) {
        const int topLeft = grid[t][l];
        for (int j = l; j < r; ++j)
          grid[t][j] = grid[t][j + 1];
        for (int i = t; i < b; ++i)
          grid[i][r] = grid[i + 1][r];
        for (int j = r; j > l; --j)
          grid[b][j] = grid[b][j - 1];
        for (int i = b; i > t; --i)
          grid[i][l] = grid[i - 1][l];
        grid[t + 1][l] = topLeft;
      }
      ++t;
      ++l;
      --b;
      --r;
    }

    return grid;
  }
};
/* code provided by PROGIEZ */

1914. Cyclically Rotating a Grid LeetCode Solution in Java

class Solution {
  public int[][] rotateGrid(int[][] grid, int k) {
    final int m = grid.length;
    final int n = grid[0].length;
    int t = 0;     // the top
    int l = 0;     // the left
    int b = m - 1; // the bottom
    int r = n - 1; // the right

    while (t < b && l < r) {
      final int elementInThisLayer = 2 * (b - t + 1) + 2 * (r - l + 1) - 4;
      final int netRotations = k % elementInThisLayer;
      for (int rotate = 0; rotate < netRotations; ++rotate) {
        final int topLeft = grid[t][l];
        for (int j = l; j < r; ++j)
          grid[t][j] = grid[t][j + 1];
        for (int i = t; i < b; ++i)
          grid[i][r] = grid[i + 1][r];
        for (int j = r; j > l; --j)
          grid[b][j] = grid[b][j - 1];
        for (int i = b; i > t; --i)
          grid[i][l] = grid[i - 1][l];
        grid[t + 1][l] = topLeft;
      }
      ++t;
      ++l;
      --b;
      --r;
    }

    return grid;
  }
}
// code provided by PROGIEZ

1914. Cyclically Rotating a Grid LeetCode Solution in Python

class Solution:
  def rotateGrid(self, grid: list[list[int]], k: int) -> list[list[int]]:
    m = len(grid)
    n = len(grid[0])
    t = 0  # the top
    l = 0  # the left
    b = m - 1  # the bottom
    r = n - 1  # the right

    while t < b and l < r:
      elementInThisLayer = 2 * (b - t + 1) + 2 * (r - l + 1) - 4
      netRotations = k % elementInThisLayer
      for _ in range(netRotations):
        topLeft = grid[t][l]
        for j in range(l, r):
          grid[t][j] = grid[t][j + 1]
        for i in range(t, b):
          grid[i][r] = grid[i + 1][r]
        for j in range(r, l, - 1):
          grid[b][j] = grid[b][j - 1]
        for i in range(b, t, -1):
          grid[i][l] = grid[i - 1][l]
        grid[t + 1][l] = topLeft
      t += 1
      l += 1
      b -= 1
      r -= 1

    return grid
# code by PROGIEZ

Additional Resources

See also  2460. Apply Operations to an Array LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.