Skip to main content

Count Total Number of Colored Cells - Solution & Explanation

MediumMath13 min readAsked at: Amazon, Microsoft, Meta +2
Practice this problem

Problem Statement

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.

Below is a pictorial representation of the state of the grid after minutes 1, 2, and 3.

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

 

Example 1:

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

Example 2:

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 <= 105

Approach Overview

Problem Overview: You start with one colored cell. Each minute, the colored region expands in four directions, forming a diamond pattern. Given n, compute the total number of colored cells after n expansions.

Approach 1: Geometric Growth Pattern (O(n) time, O(1) space)

The grid grows outward layer by layer. At minute 1, there is only one cell. Every additional minute adds a ring around the diamond. The number of new cells added at step i is 4 * (i - 1). You iterate from 2 to n, accumulating these additions into the total. This effectively sums the expansion of the diamond perimeter at each step. The method works well when you want to visualize or simulate the pattern growth and is easy to derive during interviews by writing out the first few values: 1, 5, 13, 25....

The insight comes from observing that each new layer contributes four directional arms extending from the previous boundary. Using a simple loop, you keep adding the incremental increase until reaching n. Time complexity is O(n) because you iterate once through the expansion steps, and space complexity remains O(1) since only a few variables track the running total.

Approach 2: Mathematical Formula (O(1) time, O(1) space)

The pattern from the iterative approach forms a clear mathematical sequence. Expanding the recurrence shows the total colored cells equal 1 + 4(1 + 2 + ... + (n-1)). The sum of the first k integers is k(k+1)/2, which simplifies the expression to 1 + 2n(n-1). Another equivalent form is n^2 + (n-1)^2. Both represent the number of cells inside the diamond formed after n steps.

This removes iteration entirely. You compute the value directly using constant-time arithmetic operations. Time complexity becomes O(1) and space complexity remains O(1). Problems like this often appear under Math pattern recognition where deriving the closed-form expression is the key optimization.

Recommended for interviews: Start by explaining the layer expansion pattern using the iterative reasoning. That demonstrates understanding of the grid geometry. Then derive the closed-form formula 1 + 2n(n-1) to reach the optimal O(1) solution. Interviewers typically expect the mathematical insight because it shows strong pattern recognition and familiarity with sequence summation in math and geometry-style grid problems.

Approach 1: Geometric Progression Approach

This approach utilizes the fact that as the minutes pass, the cells form a pattern around the initial blue cell. Each minute, a new layer of cells is added around the existing structure, forming a diamond shape. The nth diamond layer will add 4*(n-1) cells, considering four new cells around each previous edge cell and a single central cell for the initial step.

The function coloredCells calculates the total number of colored cells after n minutes using a loop. It accumulates the newly colored cells added each minute in a variable total.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), Space Complexity: O(1)

Try this approach in the editor →

Approach 2: Mathematical Formula Approach

This approach leverages a derived formula to directly calculate the total count of colored cells at any given minute n. The formula is derived from the observation of the expanding diamond pattern around the initial cell. Specifically, the total number of colored cells at minute n can be expressed as (2n^2 - 2n + 1), eliminating the need for iteration.

This solution in C computes the number of colored cells directly using the derived mathematical formula, optimizing calculation time by eliminating iteration.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(1), Space Complexity: O(1)

Try this approach in the editor →

Approach 3: Mathematics

We find that after the nth minute, there are a total of 2 times n - 1 columns in the grid, and the numbers on each column are respectively 1, 3, 5, cdots, 2 times n - 1, 2 times n - 3, cdots, 3, 1. The left and right parts are both arithmetic progressions, and the sum can be obtained by 2 times n times (n - 1) + 1.

The time complexity is O(1), and the space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Rust

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Geometric Progression Approach

Time Complexity: O(n), Space Complexity: O(1)

Mathematical Formula Approach

Time Complexity: O(1), Space Complexity: O(1)

Mathematics—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Geometric Growth Pattern (Iterative)O(n)O(1)When deriving the pattern step‑by‑step or explaining the grid expansion visually
Mathematical FormulaO(1)O(1)Best for production and interviews once the sequence pattern is recognized

Video Solution

Count Total Number of Colored Cells - Leetcode 2579 - Python • NeetCodeIO • 6,638 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Count Total Number of Colored Cells easy or hard?
The problem is rated Medium on LeetCode but conceptually closer to an easy math insight. Once you observe the expansion pattern of the diamond grid, deriving the formula becomes straightforward and leads to a constant-time solution.
Count Total Number of Colored Cells Python/Java solution
The Python or Java implementation typically uses the formula 1 + 2*n*(n-1). This calculates the total colored cells directly in constant time. The code is only a single arithmetic expression and works efficiently even for large values of n.
How to solve Count Total Number of Colored Cells in O(1)?
Recognize that each step adds 4(i-1) new cells, forming a sequence. Summing these additions leads to the closed form 1 + 2n(n-1). Implementing this formula requires only basic arithmetic operations, giving an O(1) time and O(1) space solution.
What is the best approach for Count Total Number of Colored Cells?
The mathematical formula approach is the best solution. The total number of colored cells after n steps follows the pattern 1 + 2n(n-1), which can also be written as n^2 + (n-1)^2. This computes the result in O(1) time and O(1) space without iterating through expansions.
Is Count Total Number of Colored Cells asked at Google/Amazon/Meta?
This problem reflects the type of mathematical pattern recognition questions asked in interviews at companies like Google, Amazon, and Meta. Variants involving grid expansion, geometric growth, or sequence derivation frequently appear in coding interviews.
What data structure is used in Count Total Number of Colored Cells?
No complex data structure is required. The solution relies purely on mathematical pattern recognition and arithmetic operations. Only a few integer variables are needed to compute the final count.
What is the time complexity of Count Total Number of Colored Cells?
Two common solutions exist. The iterative pattern approach runs in O(n) time because it sums the cells added at each expansion step. The optimal mathematical formula computes the result directly in O(1) time with constant space.

Ready to solve this problem?

Practice Count Total Number of Colored Cells with our built-in code editor and test cases.

Practice on FleetCode