You are given a 2D grid
of size m x n
and an integer k
. Your task is to shift the grid
k
times. A single shift operation is defined as follows:
grid[i][j]
moves to grid[i][j + 1]
. That is, each element moves one position to the right within its row.grid[i][n - 1]
(the last element of the i-th row) moves to grid[i + 1][0]
(the first element of the next row). That is, the last element of each row is moved to the beginning of the next row.grid[m - 1][n - 1]
(the last element of the last row) moves to grid[0][0]
(the first element of the first row). That is, the last element of the entire grid wraps around to the beginning.Your function should return the modified 2D grid
after applying the shift operation k
times.
Example:
Consider the following grid
and k
value:
grid = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
k = 1
After applying the shift operation once, the grid
becomes:
[[9, 1, 2], [3, 4, 5], [6, 7, 8]]
If we perform 9 shifts:
grid = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
k = 9
After applying the shift operation nine times, the grid
becomes:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Write a function to efficiently shift the elements of a 2D grid k
times and return the resulting grid.
Given a 2D grid of size m x n
and an integer k
, shift the grid k
times. In one shift operation:
grid[i][j]
moves to grid[i][j + 1]
.grid[i][n - 1]
moves to grid[i + 1][0]
.grid[m - 1][n - 1]
moves to grid[0][0]
.Return the 2D grid after applying shift operation k
times.
The brute force solution involves performing the shift operation k
times. In each shift, we iterate through the grid and move each element to its new position according to the rules. This method is straightforward but not the most efficient.
k
times.k
is the number of shifts, m
is the number of rows, and n
is the number of columns.A more efficient approach avoids repeated iterations by calculating the final position of each element directly. This can be done using modular arithmetic. The idea is to flatten the 2D array into a 1D array conceptually, shift the elements in the 1D array, and then map the elements back to the 2D array.
k = k % (m * n)
. This handles cases where k
is larger than the total number of elements.grid[i][j]
, calculate its new position (new_i, new_j)
after k
shifts.grid[i][j]
into the new_grid[new_i][new_j]
.new_grid
.The new position can be calculated as follows:
(i, j)
to a 1D index: index = i * n + j
.new_index = (index + k) % (m * n)
.new_index
back to 2D indices: new_i = new_index / n
and new_j = new_index % n
.m
is the number of rows and n
is the number of columns. We iterate through each element in the grid once.import java.util.ArrayList;
import java.util.List;
class Solution {
public List<List<Integer>> shiftGrid(int[][] grid, int k) {
int m = grid.length;
int n = grid[0].length;
k = k % (m * n);
List<List<Integer>> newGrid = new ArrayList<>();
for (int i = 0; i < m; i++) {
newGrid.add(new ArrayList<>());
for (int j = 0; j < n; j++) {
newGrid.get(i).add(0);
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
int index = i * n + j;
int newIndex = (index + k) % (m * n);
int newI = newIndex / n;
int newJ = newIndex % n;
newGrid.get(newI).set(newJ, grid[i][j]);
}
}
return newGrid;
}
}
The optimal solution provides a significant improvement in time complexity compared to the brute force method. By directly calculating the new positions using modular arithmetic, the algorithm avoids unnecessary repeated shifts, resulting in a more efficient solution.