[LeetCode] Count Total Number of Colored Cells

2579. 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.

Below is a pictorial representation of the state of the grid after minutes 1, 2, and 3.

1
2
3
4
5
6
7
class Solution {
public:
long long coloredCells(int n) {
if(n == 1) return 1;
return 2ll * n - 1 + 1ll * (n - 1) * (1ll + 2ll * n - 3ll);
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/03/05/PS/LeetCode/count-total-number-of-colored-cells/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.