All problems
1148MediumMath

Cells Inked After n Minutes

Tracked in this browser only
Write code

Trains the technique from

LeetCode 2579Count Total Number of Colored Cells

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

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