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

# Prison Cells After N Days Python Solution

> Tested Python solution for LeetCode 957 with 15 pytest cases. Generate a practice environment with lcpy.

LeetCode 957, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [Math](/catalog/topics/math), [Bit Manipulation](/catalog/topics/bit-manipulation). [View on LeetCode](https://leetcode.com/problems/prison-cells-after-n-days/description/).

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

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

## Problem

There are `8` prison cells in a row and each cell is either occupied or vacant.

Each day, whether the cell is occupied or vacant changes according to the following rules:

* If a cell has two adjacent neighbors that are both occupied or both vacant, then the cell becomes occupied.
* Otherwise, it becomes vacant.

Note that because the prison is a row, the first and the last cells in the row can't have two adjacent neighbors.

You are given an integer array `cells` where `cells[i] == 1` if the `ith` cell is occupied and `cells[i] == 0` if the `ith` cell is vacant, and you are given an integer `n`.

Return the state of the prison after `n` days (i.e., `n` such changes described above).

### Examples

```
Input: cells = [0,1,0,1,1,0,0,1], n = 7
Output: [0,0,1,1,0,0,0,0]
Explanation: The following table summarizes the state of the prison on each day:
Day 0: [0, 1, 0, 1, 1, 0, 0, 1]
Day 1: [0, 1, 1, 0, 0, 0, 0, 0]
Day 2: [0, 0, 0, 0, 1, 1, 1, 0]
Day 3: [0, 1, 1, 0, 0, 1, 0, 0]
Day 4: [0, 0, 0, 0, 0, 1, 0, 0]
Day 5: [0, 1, 1, 1, 0, 1, 0, 0]
Day 6: [0, 0, 1, 0, 1, 1, 0, 0]
Day 7: [0, 0, 1, 1, 0, 0, 0, 0]
```

```
Input: cells = [1,0,0,1,0,0,1,0], n = 1000000000
Output: [0,0,1,1,1,1,1,0]
```

### Constraints

* cells.length == 8
* cells\[i] is either 0 or 1.
* 1 \<= n \<= 10^9

**Follow up:** Could you solve it with `O(1)` extra space with respect to the number of days?

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(1), at most 256 distinct states so the seen-map cycle search is bounded
    # Space: O(1), the seen map holds at most 256 states
    def prison_after_n_days(self, cells: list[int], n: int) -> list[int]:
        def advance(state: tuple[int, ...]) -> tuple[int, ...]:
            return tuple(int(0 < i < 7 and state[i - 1] == state[i + 1]) for i in range(8))

        state = tuple(cells)
        seen: dict[tuple[int, ...], int] = {}
        for day in range(1, n + 1):
            state = advance(state)
            if state in seen:
                cycle = day - seen[state]
                for _ in range((n - day) % cycle):
                    state = advance(state)
                return list(state)
            seen[state] = day
        return list(state)
```

## Complexity

| Time | Space |
| - | - |
| O(1), at most 256 distinct states so the seen-map cycle search is bounded | O(1), the seen map holds at most 256 states |

## Tags


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