#2579

Count Total Number of Colored Cells

specialist · 625 · lc medium +29 · verified · 66.1% accepted · 845 likes · top 71%

Description

On an infinite 2D grid, at minute 1 you color one cell blue. Each subsequent minute, every uncolored cell sharing an edge with a blue cell also turns blue. Return the total number of blue cells after n minutes.

Example 1:

Input: n = 1
Output: 1
Explanation: After 1 minute, there is only 1 blue cell, so we return 1.

Example 2:

Input: n = 2
Output: 5
Explanation: After 2 minutes, there are 4 colored cells on the boundary and 1 in the center, so we return 5.

Code

1
2
3