Skip to content

Commit 2b6e0e8

Browse files
authored
Merge pull request neetcode-gh#1762 from AP-Repositories/patch-39
Create 1260-shift-2d-grid.js
2 parents 91cf056 + 2f8f295 commit 2b6e0e8

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

javascript/1260-shift-2d-grid.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var shiftGrid = function(grid, k) {
2+
const M = grid.length, N = grid[0].length;
3+
4+
let posToVal = (r, c) =>
5+
r * N + c;
6+
let valToPos = (v) =>
7+
[Math.floor(v / N), v % N];
8+
9+
res = [];
10+
for(let i = 0; i < M; i++)
11+
res.push([]);
12+
for(let r = 0; r < M; r++)
13+
for(let c = 0; c < N; c++) {
14+
let newVal = (posToVal(r, c) + k) % (M * N);
15+
let newRC = valToPos(newVal);
16+
res[newRC[0]][newRC[1]] = grid[r][c];
17+
}
18+
return res;
19+
};

0 commit comments

Comments
 (0)