Skip to content

Commit d163d8b

Browse files
committedApr 7, 2025
Add solution #1260
1 parent e5ad069 commit d163d8b

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed
 

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,200 LeetCode solutions in JavaScript
1+
# 1,201 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -958,6 +958,7 @@
958958
1253|[Reconstruct a 2-Row Binary Matrix](./solutions/1253-reconstruct-a-2-row-binary-matrix.js)|Medium|
959959
1254|[Number of Closed Islands](./solutions/1254-number-of-closed-islands.js)|Medium|
960960
1255|[Maximum Score Words Formed by Letters](./solutions/1255-maximum-score-words-formed-by-letters.js)|Hard|
961+
1260|[Shift 2D Grid](./solutions/1260-shift-2d-grid.js)|Easy|
961962
1261|[Find Elements in a Contaminated Binary Tree](./solutions/1261-find-elements-in-a-contaminated-binary-tree.js)|Medium|
962963
1267|[Count Servers that Communicate](./solutions/1267-count-servers-that-communicate.js)|Medium|
963964
1268|[Search Suggestions System](./solutions/1268-search-suggestions-system.js)|Medium|

‎solutions/1260-shift-2d-grid.js

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

0 commit comments

Comments
 (0)
Please sign in to comment.