LeetCode Entry
1260. Shift 2D Grid
Shift right 2d grid
1260. Shift 2D Grid easy substack youtube
https://dmitrysamoylenko.com/leetcode/

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/1426
Problem TLDR
Shift right 2d grid
Intuition
Flatten, rotate, chunk.
Approach
- don’t forget % size
Complexity
-
Time complexity: \(O(n)\)
-
Space complexity: \(O(n)\)
Code
fun shiftGrid(g: Array<IntArray>, k: Int) = g.flatMap{it.asList()}
.run{(takeLast(k%size) + dropLast(k%size)).chunked(g[0].size)}
pub fn shift_grid(g: Vec<Vec<i32>>, k: i32) -> Vec<Vec<i32>> {
let mut v = g.concat(); let k = k as usize % v.len(); v.rotate_right(k);
v.chunks(g[0].len()).map(|c|c.to_vec()).collect()
}
Comments