> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Shift 2D Grid Python Solution with Tests

> Tested Python solution for LeetCode 1260 with 18 pytest cases. Generate a practice environment with lcpy.

LeetCode 1260, [Easy](/catalog/easy). Topics: [Array](/catalog/topics/array), [Matrix](/catalog/topics/matrix), [Simulation](/catalog/topics/simulation). [View on LeetCode](https://leetcode.com/problems/shift-2d-grid/description/).

Generate this problem as a practice environment: tested reference solution, 18 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1260   # by problem number
lcpy gen -s shift_2d_grid   # by problem name
```

## Problem

Given a 2D grid of size m x n and an integer k. You need to shift the grid k times.

In one shift operation:

* Element at grid\[i]\[j] moves to grid\[i]\[j + 1].
* Element at grid\[i]\[n - 1] moves to grid\[i + 1]\[0].
* Element at grid\[m - 1]\[n - 1] moves to grid\[0]\[0].

Return the 2D grid after applying shift operation k times.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2019/11/05/e1.png)

```
Input: grid = [[1,2,3],[4,5,6],[7,8,9]], k = 1
Output: [[9,1,2],[3,4,5],[6,7,8]]
```

![Example 2](https://assets.leetcode.com/uploads/2019/11/05/e2.png)

```
Input: grid = [[3,8,1,9],[19,7,2,5],[4,6,11,10],[12,0,21,13]], k = 4
Output: [[12,0,21,13],[3,8,1,9],[19,7,2,5],[4,6,11,10]]
```

```
Input: grid = [[1,2,3],[4,5,6],[7,8,9]], k = 9
Output: [[1,2,3],[4,5,6],[7,8,9]]
```

### Constraints

* m == grid.length
* n == grid\[i].length
* 1 \<= m \<= 50
* 1 \<= n \<= 50
* -1000 \<= grid\[i]\[j] \<= 1000
* 0 \<= k \<= 100

**Follow up:**

* Can you find a O(n \* m) solution?
* Can you find an in-place solution?

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/shift_2d_grid/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/shift_2d_grid/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    def shift_grid(self, grid: list[list[int]], k: int) -> list[list[int]]:
        m, n = len(grid), len(grid[0])
        total = m * n
        k %= total
        flat = [grid[i][j] for i in range(m) for j in range(n)]
        flat = flat[-k:] + flat[:-k] if k else flat
        return [flat[i * n : (i + 1) * n] for i in range(m)]
```

## Complexity

| Time | Space |
| - | - |
| - | - |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.