Count Total Number of Colored Cells

On an infinite grid of uncolored cells, at minute 1 you color any one cell. Every minute after that, every uncolored cell that touches a colored cell (sharing a side) is colored. Given n, return the number of colored cells at the end of minute n.

30:00

Count Total Number of Colored Cells
medium
Free written solution
Topics
Companies

On an infinite grid of uncolored cells, at minute 1 you color any one cell. Every minute after that, every uncolored cell that touches a colored cell (sharing a side) is colored. Given n, return the number of colored cells at the end of minute n.

Example 1:
Input: {"n":1}
Output: 1
Constraints:
  • 1≤n≤1051 \leq n \leq 10^5

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"n":1}

Minute 1: color a single cell. We need the count after minute 1.

grid after minute 1

cells added each minute

minute 1
1
colored so far: 1
Variables
VariableValue
n1
minute1
total1
DepthFunction Call
Stack empty
0/3