From d179c1488a8c0714a1771a4ad53e9ca9adb96486 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Mon, 5 May 2025 15:51:41 +0800 Subject: [PATCH] Add solution and test-cases for problem 957 --- .../0957.Prison-Cells-After-N-Days/README.md | 41 +++++++++++------ .../Solution.go | 44 ++++++++++++++++++- .../Solution_test.go | 20 ++++----- 3 files changed, 79 insertions(+), 26 deletions(-) diff --git a/leetcode/901-1000/0957.Prison-Cells-After-N-Days/README.md b/leetcode/901-1000/0957.Prison-Cells-After-N-Days/README.md index d0744e4a0..82677e20a 100644 --- a/leetcode/901-1000/0957.Prison-Cells-After-N-Days/README.md +++ b/leetcode/901-1000/0957.Prison-Cells-After-N-Days/README.md @@ -1,28 +1,41 @@ # [957.Prison Cells After N Days][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +There are 8 prison cells in a row and each cell is either occupied or vacant. + +Each day, whether the cell is occupied or vacant changes according to the following rules: + +- If a cell has two adjacent neighbors that are both occupied or both vacant, then the cell becomes occupied. +- Otherwise, it becomes vacant. + +**Note** that because the prison is a row, the first and the last cells in the row can't have two adjacent neighbors. + +You are given an integer array `cells` where `cells[i] == 1` if the `ith` cell is occupied and `cells[i] == 0` if the `ith` cell is vacant, and you are given an integer n. + +Return the state of the prison after `n` days (i.e., n such changes described above). **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: cells = [0,1,0,1,1,0,0,1], n = 7 +Output: [0,0,1,1,0,0,0,0] +Explanation: The following table summarizes the state of the prison on each day: +Day 0: [0, 1, 0, 1, 1, 0, 0, 1] +Day 1: [0, 1, 1, 0, 0, 0, 0, 0] +Day 2: [0, 0, 0, 0, 1, 1, 1, 0] +Day 3: [0, 1, 1, 0, 0, 1, 0, 0] +Day 4: [0, 0, 0, 0, 0, 1, 0, 0] +Day 5: [0, 1, 1, 1, 0, 1, 0, 0] +Day 6: [0, 0, 1, 0, 1, 1, 0, 0] +Day 7: [0, 0, 1, 1, 0, 0, 0, 0] ``` -## 题意 -> ... - -## 题解 +**Example 2:** -### 思路1 -> ... -Prison Cells After N Days -```go ``` - +Input: cells = [1,0,0,1,0,0,1,0], n = 1000000000 +Output: [0,0,1,1,1,1,1,0] +``` ## 结语 diff --git a/leetcode/901-1000/0957.Prison-Cells-After-N-Days/Solution.go b/leetcode/901-1000/0957.Prison-Cells-After-N-Days/Solution.go index d115ccf5e..82eb431cf 100644 --- a/leetcode/901-1000/0957.Prison-Cells-After-N-Days/Solution.go +++ b/leetcode/901-1000/0957.Prison-Cells-After-N-Days/Solution.go @@ -1,5 +1,45 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(cells []int, n int) []int { + start := [8]int{} + for i := range cells { + start[i] = cells[i] + } + days := [][8]int{start} + day := 0 + cache := map[[8]int]int{ + start: day, + } + + var convert func([8]int) [8]int + convert = func(now [8]int) [8]int { + var res [8]int + res[0], res[7] = 0, 0 + for i := 1; i < 7; i++ { + if now[i-1] == now[i+1] { + res[i] = 1 + } + } + return res + } + var r [8]int + var loopStart, loopLen int + for { + day++ + r = convert(start) + if day == n { + return r[:] + } + if d, ok := cache[r]; ok { + loopStart = d + loopLen = day - d + break + } + cache[r] = day + start = r + days = append(days, r) + } + target := n - loopStart + target %= loopLen + return days[loopStart+target][:] } diff --git a/leetcode/901-1000/0957.Prison-Cells-After-N-Days/Solution_test.go b/leetcode/901-1000/0957.Prison-Cells-After-N-Days/Solution_test.go index 14ff50eb4..8c3a717bd 100644 --- a/leetcode/901-1000/0957.Prison-Cells-After-N-Days/Solution_test.go +++ b/leetcode/901-1000/0957.Prison-Cells-After-N-Days/Solution_test.go @@ -10,30 +10,30 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + cells []int + n int + expect []int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{0, 1, 0, 1, 1, 0, 0, 1}, 7, []int{0, 0, 1, 1, 0, 0, 0, 0}}, + {"TestCase2", []int{1, 0, 0, 1, 0, 0, 1, 0}, 1000000000, []int{0, 0, 1, 1, 1, 1, 1, 0}}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.cells, c.n) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.cells, c.n) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }