We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents 09de9a2 + cb01a58 commit 378e1c1Copy full SHA for 378e1c1
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