Skip to content

Commit d179c14

Browse files
committed
Add solution and test-cases for problem 957
1 parent a01e89c commit d179c14

File tree

3 files changed

+79
-26
lines changed

3 files changed

+79
-26
lines changed

leetcode/901-1000/0957.Prison-Cells-After-N-Days/README.md

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,41 @@
11
# [957.Prison Cells After N Days][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
There are 8 prison cells in a row and each cell is either occupied or vacant.
5+
6+
Each day, whether the cell is occupied or vacant changes according to the following rules:
7+
8+
- If a cell has two adjacent neighbors that are both occupied or both vacant, then the cell becomes occupied.
9+
- Otherwise, it becomes vacant.
10+
11+
**Note** that because the prison is a row, the first and the last cells in the row can't have two adjacent neighbors.
12+
13+
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.
14+
15+
Return the state of the prison after `n` days (i.e., n such changes described above).
716

817
**Example 1:**
918

1019
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
20+
Input: cells = [0,1,0,1,1,0,0,1], n = 7
21+
Output: [0,0,1,1,0,0,0,0]
22+
Explanation: The following table summarizes the state of the prison on each day:
23+
Day 0: [0, 1, 0, 1, 1, 0, 0, 1]
24+
Day 1: [0, 1, 1, 0, 0, 0, 0, 0]
25+
Day 2: [0, 0, 0, 0, 1, 1, 1, 0]
26+
Day 3: [0, 1, 1, 0, 0, 1, 0, 0]
27+
Day 4: [0, 0, 0, 0, 0, 1, 0, 0]
28+
Day 5: [0, 1, 1, 1, 0, 1, 0, 0]
29+
Day 6: [0, 0, 1, 0, 1, 1, 0, 0]
30+
Day 7: [0, 0, 1, 1, 0, 0, 0, 0]
1331
```
1432

15-
## 题意
16-
> ...
17-
18-
## 题解
33+
**Example 2:**
1934

20-
### 思路1
21-
> ...
22-
Prison Cells After N Days
23-
```go
2435
```
25-
36+
Input: cells = [1,0,0,1,0,0,1,0], n = 1000000000
37+
Output: [0,0,1,1,1,1,1,0]
38+
```
2639

2740
## 结语
2841

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,45 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(cells []int, n int) []int {
4+
start := [8]int{}
5+
for i := range cells {
6+
start[i] = cells[i]
7+
}
8+
days := [][8]int{start}
9+
day := 0
10+
cache := map[[8]int]int{
11+
start: day,
12+
}
13+
14+
var convert func([8]int) [8]int
15+
convert = func(now [8]int) [8]int {
16+
var res [8]int
17+
res[0], res[7] = 0, 0
18+
for i := 1; i < 7; i++ {
19+
if now[i-1] == now[i+1] {
20+
res[i] = 1
21+
}
22+
}
23+
return res
24+
}
25+
var r [8]int
26+
var loopStart, loopLen int
27+
for {
28+
day++
29+
r = convert(start)
30+
if day == n {
31+
return r[:]
32+
}
33+
if d, ok := cache[r]; ok {
34+
loopStart = d
35+
loopLen = day - d
36+
break
37+
}
38+
cache[r] = day
39+
start = r
40+
days = append(days, r)
41+
}
42+
target := n - loopStart
43+
target %= loopLen
44+
return days[loopStart+target][:]
545
}

leetcode/901-1000/0957.Prison-Cells-After-N-Days/Solution_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,30 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
cells []int
14+
n int
15+
expect []int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{0, 1, 0, 1, 1, 0, 0, 1}, 7, []int{0, 0, 1, 1, 0, 0, 0, 0}},
18+
{"TestCase2", []int{1, 0, 0, 1, 0, 0, 1, 0}, 1000000000, []int{0, 0, 1, 1, 1, 1, 1, 0}},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.cells, c.n)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
27+
c.expect, got, c.cells, c.n)
2828
}
2929
})
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)