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

# Count Total Number of Colored Cells

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

LeetCode 2579, [Medium](/catalog/medium). Topics: [Math](/catalog/topics/math). [View on LeetCode](https://leetcode.com/problems/count-total-number-of-colored-cells/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 2579   # by problem number
lcpy gen -s count_total_number_of_colored_cells   # by problem name
```

## Problem

There exists an infinitely large two-dimensional grid of uncolored unit cells. You are given a positive integer `n`, indicating that you must do the following routine for `n` minutes:

* At the first minute, color **any** arbitrary unit cell blue.
* Every minute thereafter, color blue **every** uncolored cell that touches a blue cell.

Return *the number of **colored cells** at the end of* `n` *minutes*.

### Examples

![Example](https://assets.leetcode.com/uploads/2023/01/10/example-copy-2.png)

```
Input: n = 1
Output: 1
Explanation: After 1 minute, there is only 1 blue cell, so we return 1.
```

```
Input: n = 2
Output: 5
Explanation: After 2 minutes, there are 4 colored cells on the boundary and 1 in the center, so we return 5.
```

### Constraints

* 1 \<= n \<= 10^5

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(1)
    # Space: O(1)
    def colored_cells(self, n: int) -> int:
        return 2 * n * n - 2 * n + 1
```

## Complexity

| Time | Space |
| - | - |
| O(1) | O(1) |

## Tags

[NeetCode All](/catalog/neetcode).


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