|
| 1 | +# [Problem 1260: Shift 2D Grid](https://leetcode.com/problems/shift-2d-grid/description/?envType=daily-question) |
| 2 | + |
| 3 | +## Initial thoughts (stream-of-consciousness) |
| 4 | +When I first read this, I think of performing the described shift operation k times. Doing that naively (simulate one shift, repeat k times) would be straightforward but could be inefficient if k were large. However, constraints show m,n ≤ 50 and k ≤ 100, so naive might actually pass here — but it's still good practice to do it in O(m*n) time regardless of k. |
| 5 | + |
| 6 | +This is basically a rotation of all elements in row-major order by k positions to the right. So flatten the 2D grid into a 1D list, rotate that list by k (with k reduced mod total elements), and then reshape back into m x n. Alternatively, we can compute index mapping directly without an intermediate list. |
| 7 | + |
| 8 | +I should be careful to take k % (m*n) and handle k == 0 specially (just return the grid as-is or a copy). |
| 9 | + |
| 10 | +## Refining the problem, round 2 thoughts |
| 11 | +Refinements: |
| 12 | +- Compute total = m * n and do k = k % total to avoid unnecessary work. |
| 13 | +- Flattening to a 1D list makes rotation easier: rotated = flat[-k:] + flat[:-k] (if k > 0). |
| 14 | +- Reconstruct the 2D result by slicing the rotated list into rows of length n. |
| 15 | +- Edge cases: |
| 16 | + - k == 0 (or k % total == 0) -> return the original grid (or a shallow copy). |
| 17 | + - m or n equals 1 -> still works with flatten+rotate approach. |
| 18 | +- Complexity: flattening is O(total) time and space; rotating using slicing is O(total); rebuilding is O(total). So overall O(m*n) time and O(m*n) extra space. We could do an in-place cycle-rotate with O(1) extra space, but it's unnecessary here and adds complexity. |
| 19 | + |
| 20 | +Now implement the clear, concise Python solution using flatten / slice / reshape. |
| 21 | + |
| 22 | +## Attempted solution(s) |
| 23 | +```python |
| 24 | +from typing import List |
| 25 | + |
| 26 | +class Solution: |
| 27 | + def shiftGrid(self, grid: List[List[int]], k: int) -> List[List[int]]: |
| 28 | + m = len(grid) |
| 29 | + n = len(grid[0]) if m > 0 else 0 |
| 30 | + total = m * n |
| 31 | + if total == 0: |
| 32 | + return grid |
| 33 | + |
| 34 | + k %= total |
| 35 | + if k == 0: |
| 36 | + # No change needed; return a copy to avoid mutating input if that matters |
| 37 | + return [row[:] for row in grid] |
| 38 | + |
| 39 | + # Flatten the grid in row-major order |
| 40 | + flat = [grid[i][j] for i in range(m) for j in range(n)] |
| 41 | + # Rotate the flattened list to the right by k |
| 42 | + rotated = flat[-k:] + flat[:-k] |
| 43 | + # Reconstruct the 2D grid |
| 44 | + res = [] |
| 45 | + for i in range(m): |
| 46 | + row = rotated[i * n : (i + 1) * n] |
| 47 | + res.append(row) |
| 48 | + return res |
| 49 | +``` |
| 50 | +- Notes about the approach: |
| 51 | + - We flatten the 2D grid to a 1D list in row-major order, perform a right rotation by k using slicing, and then reshape back to m x n. |
| 52 | + - Time complexity: O(m * n) — flattening, slicing and rebuilding each touch each element a constant number of times. |
| 53 | + - Space complexity: O(m * n) extra space for the flattened and rotated lists (can be reduced with an in-place cycle rotation, but not necessary given constraints). |
| 54 | + - Important detail: reduce k with k %= m*n to handle cases where k ≥ total elements. |
0 commit comments