> ## 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.

# Rotating the Box Python Solution with Tests

> Tested Python solution for LeetCode 1861 with 22 pytest cases. Generate a practice environment with lcpy.

LeetCode 1861, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Two Pointers](/catalog/topics/two-pointers), [Matrix](/catalog/topics/matrix). [View on LeetCode](https://leetcode.com/problems/rotating-the-box/description/).

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

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

## Problem

You are given an `m x n` matrix of characters `boxGrid` representing a side-view of a box. Each cell of the box is one of the following:

* A stone `'#'`
* A stationary obstacle `'*'`
* Empty `'.'`

The box is rotated **90 degrees clockwise**, causing some of the stones to fall due to gravity. Each stone falls down until it lands on an obstacle, another stone, or the bottom of the box. Gravity **does not** affect the obstacles' positions, and the inertia from the box's rotation **does not** affect the stones' horizontal positions.

It is **guaranteed** that each stone in `boxGrid` rests on an obstacle, another stone, or the bottom of the box.

Return *an* `n x m` *matrix representing the box after the rotation described above*.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2021/04/08/rotatingtheboxleetcodewithstones.png)

```
Input: boxGrid = [['#','.','#']]
Output: [['.'],['#'],['#']]
```

![Example 2](https://assets.leetcode.com/uploads/2021/04/08/rotatingtheboxleetcode2withstones.png)

```
Input: boxGrid = [['#','.','*','.'],['#','#','*','.']]
Output: [['#','.'],['#','#'],['*','*'],['.','.']]
```

![Example 3](https://assets.leetcode.com/uploads/2021/04/08/rotatingtheboxleetcode3withstone.png)

```
Input: boxGrid = [['#','#','*','.','*','.'],['#','#','#','*','.','.'],['#','#','#','.','#','.']]
Output: [['.','#','#'],['.','#','#'],['#','#','*'],['#','*','.'],['#','.','*'],['#','.','.']]
```

### Constraints

* `m == boxGrid.length`
* `n == boxGrid[i].length`
* `1 <= m, n <= 500`
* `boxGrid[i][j]` is either `'#'`, `'*'`, or `'.'`.

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(m * n)
    # Space: O(m * n) for the result (O(n) auxiliary)
    def rotate_the_box(self, box_grid: list[list[str]]) -> list[list[str]]:
        m, n = len(box_grid), len(box_grid[0])

        def settle(row: list[str]) -> list[str]:
            out = ["."] * n
            write = n - 1
            for i in range(n - 1, -1, -1):
                cell = row[i]
                if cell == "*":
                    out[i] = "*"
                    write = i - 1
                elif cell == "#":
                    out[write] = "#"
                    write -= 1
            return out

        settled = [settle(row) for row in box_grid]
        return [[settled[m - 1 - j][i] for j in range(m)] for i in range(n)]
```

## Complexity

| Time | Space |
| - | - |
| O(m \* n) | O(m \* n) for the result (O(n) auxiliary) |

## Tags

[NeetCode All](/catalog/neetcode).


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