Skip to content

Commit f35fe00

Browse files
authored
Added solution for 1260
1 parent 855a5eb commit f35fe00

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

01260. Shift 2D Grid.rs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
impl Solution {
2+
pub fn shift_grid(grid: Vec<Vec<i32>>, k: i32) -> Vec<Vec<i32>> {
3+
let rows = grid.len();
4+
let cols = grid[0].len();
5+
let mut old_grid = grid.to_vec();
6+
let mut new_grid = grid.to_vec();
7+
8+
// Do the entire operation k times
9+
for x in 0..k {
10+
// Shift entire column by 1
11+
for y in 0..rows {
12+
for z in 0..cols {
13+
new_grid[y][(z + 1) % cols] = old_grid[y][z];
14+
}
15+
}
16+
17+
// Shift entire first row by -1
18+
for y in 0..rows {
19+
new_grid[y][0] = old_grid[(y + rows - 1) % rows][cols - 1];
20+
}
21+
22+
// Update old grid to new grid
23+
old_grid = new_grid.to_vec();
24+
}
25+
return new_grid;
26+
}
27+
}

0 commit comments

Comments
 (0)