/**
* Problem: LeetCode 1914 - Cyclically Rotating a Grid
* Difficulty: Medium
* Language: Java
* Author: CodeByTushu
*/
import java.util.*;
class Solution {
public int[][] rotateGrid(int[][] grid, int k) {
int m = grid.length;
int n = grid[0].length;
int layers = Math.min(m, n) / 2;
for (int l = 0; l < layers; l++) {
int r1 = l, c1 = l;
int r2 = m - 1 - l, c2 = n - 1 - l;
// 1. Collect coordinates along counter-clockwise perimeter
List<int[]> positions = new ArrayList<>();
// Left column: top to bottom
for (int r = r1; r <= r2; r++) positions.add(new int[]{r, c1});
// Bottom row: left to right
for (int c = c1 + 1; c <= c2; c++) positions.add(new int[]{r2, c});
// Right column: bottom to top
for (int r = r2 - 1; r >= r1; r--) positions.add(new int[]{r, c2});
// Top row: right to left
for (int c = c2 - 1; c >= c1 + 1; c--) positions.add(new int[]{r1, c});
int L = positions.size();
int[] vals = new int[L];
for (int i = 0; i < L; i++) {
int[] pos = positions.get(i);
vals[i] = grid[pos[0]][pos[1]];
}
// 2. Rotate counter-clockwise by k % L
int shift = k % L;
for (int i = 0; i < L; i++) {
int targetIdx = (i + shift) % L;
int[] targetPos = positions.get(targetIdx);
grid[targetPos[0]][targetPos[1]] = vals[i];
}
}
return grid;
}
}
"""
Problem: LeetCode 1914 - Cyclically Rotating a Grid
Difficulty: Medium
Language: Python
Author: CodeByTushu
"""
from typing import List
class Solution:
def rotateGrid(self, grid: List[List[int]], k: int) -> List[List[int]]:
m, n = len(grid), len(grid[0])
layers = min(m, n) // 2
for l in range(layers):
r1, c1 = l, l
r2, c2 = m - 1 - l, n - 1 - l
# Collect coordinates along counter-clockwise perimeter
positions = []
# 1. Left col: top to bottom
for r in range(r1, r2 + 1):
positions.append((r, c1))
# 2. Bottom row: left to right
for c in range(c1 + 1, c2 + 1):
positions.append((r2, c))
# 3. Right col: bottom to top
for r in range(r2 - 1, r1 - 1, -1):
positions.append((r, c2))
# 4. Top row: right to left
for c in range(c2 - 1, c1, -1):
positions.append((r1, c))
L = len(positions)
vals = [grid[r][c] for r, c in positions]
shift = k % L
# Write rotated values back into grid
for i in range(L):
tr, tc = positions[(i + shift) % L]
grid[tr][tc] = vals[i]
return grid
/**
* Problem: LeetCode 1914 - Cyclically Rotating a Grid
* Difficulty: Medium
* Language: C++
* Author: CodeByTushu
*/
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
vector<vector<int>> rotateGrid(vector<vector<int>>& grid, int k) {
int m = grid.size();
int n = grid[0].size();
int layers = min(m, n) / 2;
for (int l = 0; l < layers; ++l) {
int r1 = l, c1 = l;
int r2 = m - 1 - l, c2 = n - 1 - l;
// Collect coordinates along counter-clockwise perimeter
vector<pair<int, int>> positions;
// 1. Left col: top to bottom
for (int r = r1; r <= r2; ++r) positions.push_back({r, c1});
// 2. Bottom row: left to right
for (int c = c1 + 1; c <= c2; ++c) positions.push_back({r2, c});
// 3. Right col: bottom to top
for (int r = r2 - 1; r >= r1; --r) positions.push_back({r, c2});
// 4. Top row: right to left
for (int c = c2 - 1; c >= c1 + 1; --c) positions.push_back({r1, c});
int L = positions.size();
vector<int> vals(L);
for (int i = 0; i < L; ++i) {
vals[i] = grid[positions[i].first][positions[i].second];
}
int shift = k % L;
for (int i = 0; i < L; ++i) {
auto [tr, tc] = positions[(i + shift) % L];
grid[tr][tc] = vals[i];
}
}
return grid;
}
};
/**
* Problem: LeetCode 1914 - Cyclically Rotating a Grid
* Difficulty: Medium
* Language: JavaScript
* Author: CodeByTushu
*/
var rotateGrid = function(grid, k) {
const m = grid.length;
const n = grid[0].length;
const layers = Math.min(m, n) / 2;
for (let l = 0; l < layers; l++) {
const r1 = l, c1 = l;
const r2 = m - 1 - l, c2 = n - 1 - l;
const positions = [];
// 1. Left col: top to bottom
for (let r = r1; r <= r2; r++) positions.push([r, c1]);
// 2. Bottom row: left to right
for (let c = c1 + 1; c <= c2; c++) positions.push([r2, c]);
// 3. Right col: bottom to top
for (let r = r2 - 1; r >= r1; r--) positions.push([r, c2]);
// 4. Top row: right to left
for (let c = c2 - 1; c >= c1 + 1; c--) positions.push([r1, c]);
const L = positions.length;
const vals = new Array(L);
for (let i = 0; i < L; i++) {
vals[i] = grid[positions[i][0]][positions[i][1]];
}
const shift = k % L;
for (let i = 0; i < L; i++) {
const [tr, tc] = positions[(i + shift) % L];
grid[tr][tc] = vals[i];
}
}
return grid;
};