All prompts are owned by LeetCode. To view the prompt, click the title link above.
First completed : March 05, 2025
Last updated : March 05, 2025
Related Topics : Math
Acceptance Rate : 66.28 %
Notes
n output 1 1 2 1 + 4 = 5 3 1 + 4 + 8 = 13 4 1 + 4 + 8 + 12 = 25 5 1 + 4 + 8 + 12 + 16 = 41
The difference increases by 4 each time
Iterative
$O(n)$ Math
$O(1)$
class Solution:
def coloredCells(self, n: int) -> int:
return 1 + sum(layer for layer in range(4, n * 4, 4))
class Solution:
def coloredCells(self, n: int) -> int:
return 1 + 4 * n // 2 * (n - 1)