diff --git a/leetcode/2501-2600/2579.Count-Total-Number-of-Colored-Cells/1.png b/leetcode/2501-2600/2579.Count-Total-Number-of-Colored-Cells/1.png new file mode 100644 index 000000000..cdcc3dbd8 Binary files /dev/null and b/leetcode/2501-2600/2579.Count-Total-Number-of-Colored-Cells/1.png differ diff --git a/leetcode/2501-2600/2579.Count-Total-Number-of-Colored-Cells/README.md b/leetcode/2501-2600/2579.Count-Total-Number-of-Colored-Cells/README.md new file mode 100644 index 000000000..6b109edb3 --- /dev/null +++ b/leetcode/2501-2600/2579.Count-Total-Number-of-Colored-Cells/README.md @@ -0,0 +1,37 @@ +# [2579.Count Total Number of Colored Cells][title] + +## 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: + +- 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](./1.png) + +Return the number of colored cells at the end of 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. +``` + +## 结语 + +如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me] + +[title]: https://leetcode.com/problems/grid-illumination/ +[me]: https://github.com/kylesliu/awesome-golang-algorithm diff --git a/leetcode/2501-2600/2579.Count-Total-Number-of-Colored-Cells/Solution.go b/leetcode/2501-2600/2579.Count-Total-Number-of-Colored-Cells/Solution.go index d115ccf5e..077bb8a17 100755 --- a/leetcode/2501-2600/2579.Count-Total-Number-of-Colored-Cells/Solution.go +++ b/leetcode/2501-2600/2579.Count-Total-Number-of-Colored-Cells/Solution.go @@ -1,5 +1,12 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(n int) int64 { + ans := int64(1) + var add int64 + for i := 2; i <= n; i++ { + add = int64(i-1) * 4 + ans += add + } + + return ans } diff --git a/leetcode/2501-2600/2579.Count-Total-Number-of-Colored-Cells/Solution_test.go b/leetcode/2501-2600/2579.Count-Total-Number-of-Colored-Cells/Solution_test.go index 14ff50eb4..e561b7e20 100755 --- a/leetcode/2501-2600/2579.Count-Total-Number-of-Colored-Cells/Solution_test.go +++ b/leetcode/2501-2600/2579.Count-Total-Number-of-Colored-Cells/Solution_test.go @@ -10,12 +10,13 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs int + expect int64 }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", 1, 1}, + {"TestCase2", 2, 5}, + {"TestCase3", 3, 13}, + {"TestCase4", 4, 25}, } // 开始测试 @@ -30,10 +31,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }