diff --git a/leetCode/matrix/shift-2d-grid.rs b/leetCode/matrix/shift-2d-grid.rs new file mode 100644 index 0000000..4cd4a5a --- /dev/null +++ b/leetCode/matrix/shift-2d-grid.rs @@ -0,0 +1,32 @@ +/// https://leetcode.com/problems/shift-2d-grid/ +impl Solution { + #[inline] + fn get_x_y(n: usize, pos: usize) -> (usize, usize) { + (pos / n, pos % n) + } + + pub fn shift_grid(grid: Vec>, k: i32) -> Vec> { + let n = grid[0].len(); + let m = grid.len(); + let k = (k as usize) %(n*m); + let k = (n * m) - k; + + if k == 0 { + return grid; + } + + let mut resp = Vec::new(); + let mut pos = 0; + for i in 0..m { + resp.push(Vec::new()); + for j in 0..n { + let (x, y) = Self::get_x_y(n, (pos + k) % (n*m)); + // println!("pos: {pos}, k: {k}, x: {x}, y: {y}, n: {n}, m: {m}"); + resp[i].push(grid[x][y]); + pos += 1; + } + } + + resp + } +}