Leetcode Problem 1260. Shift 2D Grid

1260. Shift 2D Grid

Leetcode Solutions

Approach: Using Modulo Arithmetic

  1. Calculate the total number of elements in the grid (total_elements = m * n).
  2. For each element in the grid, calculate its one-dimensional index (index = i * n + j).
  3. Add k to the index and take modulo by total_elements to get the new index after shifts (new_index = (index + k) % total_elements).
  4. Calculate the new row and column from the new index (new_row = new_index // n, new_col = new_index % n).
  5. Create a new 2D grid and place each element in its new position.
  6. Return the new grid.
UML Thumbnail

Approach: Simulation

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...