LeetCode Entry

1260. Shift 2D Grid

20.07.2026 easy 2026 kotlin rust

Shift right 2d grid

1260. Shift 2D Grid easy substack youtube

https://dmitrysamoylenko.com/leetcode/

20.07.2026.webp

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