2579. Count Total Number of Colored Cells ¶ Time: $O(1)$ Space: $O(1)$ C++JavaPython 1 2 3 4 5 6class Solution { public: long long coloredCells(int n) { return static_cast<long>(n) * n + static_cast<long>(n - 1) * (n - 1); } }; 1 2 3 4 5class Solution { public long coloredCells(int n) { return 1L * n * n + 1L * (n - 1) * (n - 1); } } 1 2 3class Solution: def coloredCells(self, n: int) -> int: return n**2 + (n - 1)**2