Show problems as
Classic problem #2579MediumMath

Count Total Number of Colored Cells

Real-world version: Cells Inked After n Minutes
Tracked in this browser only
Read Count Total Number of Colored Cells on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

An endless sheet of square cells starts blank. In the first minute a single cell is inked. In every minute after that, every blank cell sharing a side with an inked cell becomes inked as well.

Return how many cells are inked at the end of minute n.

Examples

Example 1

Input
n = 3
Output
13

One cell after the first minute, its four side neighbours after the second for five in all, and the eight cells ringing those after the third for thirteen.

Example 2

Input
n = 4
Output
25

A fourth minute adds the ring of twelve cells one step further out, bringing the total to twenty-five.

Example 3

Input
n = 7
Output
85

After seven minutes the diamond reaches six steps from the middle in every direction.

Constraints

  • 1 <= n <= 10^5

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def colored_cells(n: int) -> int:
Java
public long coloredCells(int n)