1
var rotateGrid = function (grid, k) {3
const n = grid[0].length;11
while (startRow <= endRow && startCol <= endCol) {12
let currRowStart = startRow;13
let currRowEnd = endRow;15
let currColStart = startCol;16
let currColEnd = endCol;21
for (let col = currColStart; col <= currColEnd; ++col) {22
const val = grid[currRowStart][col];28
for (let row = currRowStart; row <= currRowEnd; ++row) {29
const val = grid[row][currColEnd];35
for (let col = currColEnd; col >= currColStart; --col) {36
const val = grid[currRowEnd][col];42
for (let row = currRowEnd; row >= currRowStart; --row) {43
const val = grid[row][currColStart];48
const size = vals.length;49
const remK = k % size;53
for (let i = 0; i < size; ++i) {54
const movedIdx = (i + remK) % size;55
movedVals[i] = vals[movedIdx];60
currRowStart = startRow;63
currColStart = startCol;67
for (let col = currColStart; col <= currColEnd; ++col) {68
grid[currRowStart][col] = movedVals[i++];73
for (let row = currRowStart; row <= currRowEnd; ++row) {74
grid[row][currColEnd] = movedVals[i++];79
for (let col = currColEnd; col >= currColStart; --col) {80
grid[currRowEnd][col] = movedVals[i++];85
for (let row = currRowEnd; row >= currRowStart; --row) {86
grid[row][currColStart] = movedVals[i++];90
// update the boundaries for the next layer91
startRow = currRowStart;94
startCol = currColStart;