Count Total Number of Colored Cells - Problem
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.
Return the number of colored cells at the end of n minutes.
Input & Output
Example 1 — Basic Case
$
Input:
n = 1
›
Output:
1
💡 Note:
At minute 1, we color the initial cell. Total colored cells = 1.
Example 2 — Small Diamond
$
Input:
n = 2
›
Output:
5
💡 Note:
Minute 1: 1 cell. Minute 2: original cell + 4 adjacent cells = 5 total.
Example 3 — Growing Pattern
$
Input:
n = 3
›
Output:
13
💡 Note:
Forms diamond shape: 1 + 4 + 8 = 13 cells after 3 minutes.
Constraints
- 1 ≤ n ≤ 105
Visualization
Tap to expand
Understanding the Visualization
1
Input
Number of minutes n = 3
2
Process
Cells spread outward each minute forming diamond
3
Output
Total colored cells = 13
Key Takeaway
🎯 Key Insight: The spreading pattern forms a predictable diamond shape that can be calculated using the mathematical formula 2n² - 2n + 1
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code