Mid/SeniorMath

Count Total Number of Colored Cells

There exists an infinitely large two-dimensional grid of uncolored unit cells. You are given a positive integer n, indicating that you must do the following routine for n minutes:

  • At the first minute, color any arbitrary unit cell blue.
  • Every minute thereafter, color blue every uncolored cell that touches a blue cell.

Return the number of colored cells at the end of n minutes.

Example 1
Inputn = 1
Output1
After 1 minute, there is only 1 blue cell, so we return 1.
Example 2
Inputn = 2
Output5
After 2 minutes, there are 4 colored cells on the boundary and 1 in the center, so we return 5.

Constraints

  • 1 <= n <= 10^5

Asked at 5 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate