Skip to content

Commit 65be5d1

Browse files
committed
update: 2579. Count Total Number of Colored Cells
1 parent 8319f5c commit 65be5d1

2 files changed

Lines changed: 70 additions & 0 deletions

File tree

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
---
2+
title: 2579. Count Total Number of Colored Cells
3+
description: 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
4+
keywords: [LeetCode, 2579. Count Total Number of Colored Cells, Medium,Math]
5+
---
6+
7+
# [{frontMatter.title}](https://leetcode.com/problems/count-total-number-of-colored-cells)
8+
:::danger Medium
9+
:::
10+
11+
import Tabs from "@theme/Tabs";
12+
import TabItem from "@theme/TabItem";
13+
14+
<Tabs>
15+
<TabItem value="description" label="題目描述" default>
16+
## Description
17+
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:
18+
19+
At the first minute, color any arbitrary unit cell blue.
20+
Every minute thereafter, color blue every uncolored cell that touches a blue cell.
21+
Below is a pictorial representation of the state of the grid after minutes 1, 2, and 3.
22+
23+
![](https://assets.leetcode.com/uploads/2023/01/10/example-copy-2.png)
24+
25+
Return the number of colored cells at the end of n minutes.
26+
27+
28+
#### Example 1:
29+
Input: n = 1
30+
Output: 1
31+
Explanation: After 1 minute, there is only 1 blue cell, so we return 1.
32+
33+
#### Example 2:
34+
Input: n = 2
35+
Output: 5
36+
Explanation: After 2 minutes, there are 4 colored cells on the boundary and 1 in the center, so we return 5.
37+
38+
39+
## Constraints:
40+
```
41+
1 <= n <= 105
42+
```
43+
</TabItem>
44+
<TabItem value="solution" label="解答">
45+
## Solution
46+
```js
47+
/**
48+
* @param {number} n
49+
* @return {number}
50+
*/
51+
var coloredCells = function (n) {
52+
return 2 * n * (n - 1) + 1;
53+
};
54+
55+
```
56+
</TabItem>
57+
</Tabs>
58+
59+
60+
## 解題思路
61+
看了 Discussion 提到這題其實是個數學問題,套公式就能寫出時間複雜度空間複雜度皆為 O(1) 的完美答案
62+
## 心得
63+
窩...數學好爛...
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* @param {number} n
3+
* @return {number}
4+
*/
5+
var coloredCells = function (n) {
6+
return 2 * n * (n - 1) + 1;
7+
};

0 commit comments

Comments
 (0)