Count Total Number of Colored Cells (LeetCode 2579): Solution and Explanation

Medium
Math

A single colored cell spreads to its side neighbors every minute. Count the colored cells after n minutes. The shape is a diamond that gains a ring of 4 * (k - 1) cells at minute k, which sums to 1 + 2n(n - 1).

Intuition

Draw the first few minutes. One cell, then a plus sign of 5, then a diamond of 13. Every cell within Manhattan distance n - 1 of the start is colored after minute n.

Count what each minute adds rather than the whole shape. At minute k the new cells are those at distance exactly k - 1, and a diamond ring at distance d has 4 * d cells: d along each of its four slanted edges.

So the total is 1 + 4 * (1 + 2 + ... + (n - 1)). The bracket is the triangular number n(n - 1)/2, which gives 1 + 2n(n - 1). For n = 10^5 that is about 2 * 10^10, so it needs 64-bit arithmetic.

Approach: Closed form from the ring sizes

  1. Minute 1 colors 1 cell.
  2. Minute k (k >= 2) adds a ring of 4 * (k - 1) cells.
  3. Sum the rings: 1 + 4 * n(n - 1)/2 = 1 + 2 * n * (n - 1).
  4. Compute it in 64-bit (2L, 2LL or int64) so large n does not overflow.

Step-by-step example

n = 3
  1. Minute 1: the starting cell. Total 1.
  2. Minute 2: its 4 neighbors turn on, a ring of 4 * 1 = 4. Total 5.
  3. Minute 3: the next ring has 4 * 2 = 8 cells. Total 13.
  4. Check with the formula: 1 + 2 * 3 * 2 = 13. Return 13.

Code: Python, Java, C++

1class Solution:
2    def coloredCells(self, n: int) -> int:
3        # minute k adds a ring of 4 * (k - 1) cells, so total = 1 + 4 * (1 + 2 + ... + (n - 1))
4        return 1 + 2 * n * (n - 1)
Press play on the visualizer and the running line lights up here.

Time and space complexity

Work as the input growsinput size n →steps
Closed form from the ring sizes O(1)
Two squares O(1)
Add the rings in a loop O(n)
Time

O(1)

A fixed number of multiplications and additions.

Space

O(1)

No extra memory.

Other approaches

Add the rings in a loop (time O(n), space O(1))

Start at 1 and add 4 * (k - 1) for k from 2 to n. It is easy to verify against the drawing and fast enough for n up to 10^5, but it is O(n) where the formula is O(1).

Two squares (time O(1), space O(1))

The diamond is an n by n square of cells interleaved with an (n - 1) by (n - 1) square, so the count is n^2 + (n - 1)^2, which expands to the same 2n^2 - 2n + 1.

Common mistakes

  • Computing 2 * n * (n - 1) in 32-bit ints. For n = 10^5 it is about 2 * 10^10 and overflows; promote to 64-bit before multiplying.
  • Counting the ring as 4 * k instead of 4 * (k - 1). Minute 2 adds 4 cells, not 8.
  • Trying to simulate the grid. The grid is unbounded and the count grows quadratically, so simulation is far too slow and unnecessary.

Frequently asked questions

Why is each ring 4 * (k - 1) cells?

The ring at distance d from the center is a diamond outline. Each of its four edges has d cells (not counting the shared corner at its start), so the outline has 4d cells, and minute k adds the ring at d = k - 1.

Does the starting cell's position matter?

No. The grid is infinite in every direction, so the shape is the same diamond wherever it starts.

Is JavaScript safe without BigInt?

Yes. The largest answer is under 2 * 10^10, far below 2^53, so ordinary numbers hold it exactly.