Skip to content

Commit b3e2ac3

Browse files
authored
Merge pull request #1140 from 0xff-dev/2579
Add solution and test-cases for problem 2579
2 parents cbb1936 + 3e17671 commit b3e2ac3

File tree

4 files changed

+54
-9
lines changed

4 files changed

+54
-9
lines changed
15.3 KB
Loading
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# [2579.Count Total Number of Colored Cells][title]
2+
3+
## Description
4+
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:
5+
6+
- At the first minute, color **any** arbitrary unit cell blue.
7+
- Every minute thereafter, color blue **every** uncolored cell that touches a blue cell.
8+
9+
Below is a pictorial representation of the state of the grid after minutes 1, 2, and 3.
10+
11+
12+
![1](./1.png)
13+
14+
Return the number of colored cells at the end of n minutes.
15+
16+
**Example 1:**
17+
18+
```
19+
Input: n = 1
20+
Output: 1
21+
Explanation: After 1 minute, there is only 1 blue cell, so we return 1.
22+
```
23+
24+
**Example 2:**
25+
26+
```
27+
Input: n = 2
28+
Output: 5
29+
Explanation: After 2 minutes, there are 4 colored cells on the boundary and 1 in the center, so we return 5.
30+
```
31+
32+
## 结语
33+
34+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
35+
36+
[title]: https://leetcode.com/problems/grid-illumination/
37+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(n int) int64 {
4+
ans := int64(1)
5+
var add int64
6+
for i := 2; i <= n; i++ {
7+
add = int64(i-1) * 4
8+
ans += add
9+
}
10+
11+
return ans
512
}

leetcode/2501-2600/2579.Count-Total-Number-of-Colored-Cells/Solution_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs int
14+
expect int64
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", 1, 1},
17+
{"TestCase2", 2, 5},
18+
{"TestCase3", 3, 13},
19+
{"TestCase4", 4, 25},
1920
}
2021

2122
// 开始测试
@@ -30,10 +31,10 @@ func TestSolution(t *testing.T) {
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
 (0)