We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 4ff28b9 commit cb01a58Copy full SHA for cb01a58
kotlin/1260-shift-2d-grid.kt
@@ -0,0 +1,21 @@
1
+class Solution {
2
+ fun shiftGrid(grid: Array<IntArray>, k: Int): List<List<Int>> {
3
+ val m = grid.size
4
+ val n = grid[0].size
5
+
6
+ fun posToVal(r: Int, c: Int) = r * n + c
7
8
+ fun valToPos(v: Int) = (v / n) to (v % n)
9
10
+ val res = MutableList<MutableList<Int>> (m) { MutableList<Int> (n) { 0 } }
11
+ for (r in 0 until m) {
12
+ for (c in 0 until n) {
13
+ val newVal = (posToVal(r, c) + k) % (m * n)
14
+ val (newR, newC) = valToPos(newVal)
15
+ res[newR][newC] = grid[r][c]
16
+ }
17
18
19
+ return res
20
21
+}
0 commit comments