Skip to content

Commit cb01a58

Browse files
authored
Create 1260-shift-2d-grid.kt
1 parent 4ff28b9 commit cb01a58

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

Diff for: kotlin/1260-shift-2d-grid.kt

+21
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)