File tree 2 files changed +39
-1
lines changed
2 files changed +39
-1
lines changed Original file line number Diff line number Diff line change 1
- # 1,200 LeetCode solutions in JavaScript
1
+ # 1,201 LeetCode solutions in JavaScript
2
2
3
3
[ https://leetcodejavascript.com ] ( https://leetcodejavascript.com )
4
4
958
958
1253|[ Reconstruct a 2-Row Binary Matrix] ( ./solutions/1253-reconstruct-a-2-row-binary-matrix.js ) |Medium|
959
959
1254|[ Number of Closed Islands] ( ./solutions/1254-number-of-closed-islands.js ) |Medium|
960
960
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|
961
962
1261|[ Find Elements in a Contaminated Binary Tree] ( ./solutions/1261-find-elements-in-a-contaminated-binary-tree.js ) |Medium|
962
963
1267|[ Count Servers that Communicate] ( ./solutions/1267-count-servers-that-communicate.js ) |Medium|
963
964
1268|[ Search Suggestions System] ( ./solutions/1268-search-suggestions-system.js ) |Medium|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments