Skip to content

Latest commit

 

History

History
63 lines (50 loc) · 1.22 KB

_2579. Count Total Number of Colored Cells.md

File metadata and controls

63 lines (50 loc) · 1.22 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


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

V1

Iterative $O(n)$

V2

Math $O(1)$


Solutions

Python

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)