2579. Count Total Number of Colored Cells LeetCode Solution
In this guide, you will get 2579. Count Total Number of Colored Cells LeetCode Solution with the best time and space complexity. The solution to Count Total Number of Colored Cells problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Count Total Number of Colored Cells solution in C++
- Count Total Number of Colored Cells solution in Java
- Count Total Number of Colored Cells solution in Python
- Additional Resources

Problem Statement of Count Total Number of Colored Cells
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
Complexity Analysis
- Time Complexity: O(1)
- Space Complexity: O(1)
2579. Count Total Number of Colored Cells LeetCode Solution in C++
class Solution {
public:
long long coloredCells(int n) {
return static_cast<long>(n) * n + static_cast<long>(n - 1) * (n - 1);
}
};
/* code provided by PROGIEZ */
2579. Count Total Number of Colored Cells LeetCode Solution in Java
class Solution {
public long coloredCells(int n) {
return 1L * n * n + 1L * (n - 1) * (n - 1);
}
}
// code provided by PROGIEZ
2579. Count Total Number of Colored Cells LeetCode Solution in Python
class Solution:
def coloredCells(self, n: int) -> int:
return n**2 + (n - 1)**2
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.