2579. Count Total Number of Colored Cells - Explanation

Problem Link

Description

An infinite square grid starts with one colored cell at minute 1. At each following minute, color every uncolored cell that shares an edge with a cell colored during an earlier minute. All additions within a minute happen simultaneously. Return the total number of colored cells after minute n.

Example 1:

Count Total Number of Colored Cells, example 1: illustration of the example

Input: n = 3

Output: 13

Explanation: The first minute colors 1 cell. The second adds its 4 edge-neighbors, and the third adds 8 more cells, for a total of 1 + 4 + 8 = 13.

Example 2:

Count Total Number of Colored Cells, example 2: illustration of the example

Input: n = 4

Output: 25

Explanation: The first three minutes color 13 cells. The fourth minute adds a new outer layer of 12 cells, bringing the total to 25.

Example 3:

Count Total Number of Colored Cells, example 3: illustration of the example

Input: n = 5

Output: 41

Explanation: After minute 4 there are 25 colored cells. Minute 5 adds the next outer layer of 16 cells, so the total becomes 41.

Constraints:

  • 1 <= n <= 100000


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.