-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path1260-shift-2d-grid.js
37 lines (33 loc) · 996 Bytes
/
1260-shift-2d-grid.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
* 1260. Shift 2D Grid
* https://leetcode.com/problems/shift-2d-grid/
* Difficulty: Easy
*
* Given a 2D grid of size m x n and an integer k. You need to shift the grid k times.
*
* In one shift operation:
* - Element at grid[i][j] moves to grid[i][j + 1].
* - Element at grid[i][n - 1] moves to grid[i + 1][0].
* - Element at grid[m - 1][n - 1] moves to grid[0][0].
*
* Return the 2D grid after applying shift operation k times.
*/
/**
* @param {number[][]} grid
* @param {number} k
* @return {number[][]}
*/
var shiftGrid = function(grid, k) {
const rows = grid.length;
const cols = grid[0].length;
const total = rows * cols;
if (k === 0 || k % total === 0) return grid;
const effectiveShifts = k % total;
const flat = grid.flat();
const shifted = [...flat.slice(-effectiveShifts), ...flat.slice(0, -effectiveShifts)];
const result = [];
for (let i = 0; i < rows; i++) {
result.push(shifted.slice(i * cols, (i + 1) * cols));
}
return result;
};