2649. Count Total Number of Colored Cells
My accepted Python solution to LeetCode problem 2649, Count Total Number of Colored Cells, running in 0ms.
- Difficulty: Medium
- Python
- Runtime 0ms
- Memory 17.1MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 0ms, memory 17.1MB, accepted 2025-12-31.
class Solution:
def coloredCells(self, n: int) -> int:
# Pattern:
# n=1: 1
# n=2: 1 + 4 = 5
# n=3: 1 + 4 + 8 = 13
# n=4: 1 + 4 + 8 + 12 = 25
# Each step adds 4*(n-1) cells
# Formula: 1 + 4*(1+2+...+(n-1)) = 1 + 4*(n-1)*n/2 = 1 + 2*(n-1)*n
# = 1 + 2n^2 - 2n = 2n^2 - 2n + 1
return 2 * n * n - 2 * n + 1