Skip to content
LC-2579 Medium LeetCode

2579. Count Total Number of Colored Cells

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 66% Topics: Math
View full problem on LeetCode

Reading material

Reference solution (spoiler · python)
# Time:  O(1)
# Space: O(1)

# math
class Solution(object):
    def coloredCells(self, n):
        """
        :type n: int
        :rtype: int
        """
        return n**2+(n-1)**2


# Time:  O(1)
# Space: O(1)
# math
class Solution2(object):
    def coloredCells(self, n):
        """
        :type n: int
        :rtype: int
        """
        return (1+(1+2*(n-1)))*n//2*2-(2*n-1)

Solution from kamyu104/LeetCode-Solutions · MIT