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:
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 <= 105Problem 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.
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.
Time Complexity: O(n), Space Complexity: O(1)
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.
Time Complexity: O(1), Space Complexity: O(1)
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).
| Approach | Complexity |
|---|---|
| Geometric Progression Approach | Time Complexity: O(n), Space Complexity: O(1) |
| Mathematical Formula Approach | Time Complexity: O(1), Space Complexity: O(1) |
| Mathematics | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Geometric Growth Pattern (Iterative) | O(n) | O(1) | When deriving the pattern step‑by‑step or explaining the grid expansion visually |
| Mathematical Formula | O(1) | O(1) | Best for production and interviews once the sequence pattern is recognized |
Count Total Number of Colored Cells - Leetcode 2579 - Python • NeetCodeIO • 6,590 views views
Watch 9 more video solutions →Practice Count Total Number of Colored Cells with our built-in code editor and test cases.
Practice on FleetCode