Skip to content

Add solution and test-cases for problem 957 #1196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 27 additions & 14 deletions leetcode/901-1000/0957.Prison-Cells-After-N-Days/README.md
Original file line number Diff line number Diff line change
@@ -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]
```

## 结语

Expand Down
44 changes: 42 additions & 2 deletions leetcode/901-1000/0957.Prison-Cells-After-N-Days/Solution.go
Original file line number Diff line number Diff line change
@@ -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][:]
}
20 changes: 10 additions & 10 deletions leetcode/901-1000/0957.Prison-Cells-After-N-Days/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
}
Loading