Skip to content

Commit

Permalink
Add solution and test-cases for problem 1269
Browse files Browse the repository at this point in the history
  • Loading branch information
0xff-dev committed Oct 15, 2023
1 parent 485c507 commit c40186b
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,28 +1,38 @@
# [1269.Number of Ways to Stay in the Same Place After Some Steps][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
You have a pointer at index `0` in an array of size `arrLen`. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).

Given two integers `steps` and `arrLen`, return the number of ways such that your pointer is still at index `0` after **exactly** steps steps. Since the answer may be too large, return it **modulo** `10^9 + 7`.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: steps = 3, arrLen = 2
Output: 4
Explanation: There are 4 differents ways to stay at index 0 after 3 steps.
Right, Left, Stay
Stay, Right, Left
Right, Stay, Left
Stay, Stay, Stay
```

## 题意
> ...
**Example 2:**

## 题解

### 思路1
> ...
Number of Ways to Stay in the Same Place After Some Steps
```go
```
Input: steps = 2, arrLen = 4
Output: 2
Explanation: There are 2 differents ways to stay at index 0 after 2 steps
Right, Left
Stay, Stay
```

**Example 3:**

```
Input: steps = 4, arrLen = 2
Output: 8
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,43 @@
package Solution

func Solution(x bool) bool {
return x
const mod1269 = 1000000007

func Solution(steps int, arrLen int) int {
dp := make([][]int, arrLen)
if steps <= arrLen {
arrLen = steps
}
for i := 0; i < arrLen; i++ {
dp[i] = make([]int, steps+1)
}
for i := 0; i < arrLen; i++ {
for j := 0; j <= steps; j++ {
dp[i][j] = -1
}
}
var dirs = []int{-1, 0, 1}
var dfs func(int, int) int
dfs = func(index, useSteps int) int {
if useSteps == 0 {
if index == 0 {
return 1
}
return 0
}
if index >= arrLen {
return 0
}
if dp[index][useSteps] != -1 {
return dp[index][useSteps]
}
ans := 0
for _, dir := range dirs {
if ni := index + dir; ni >= 0 {
ans = (ans + dfs(ni, useSteps-1)) % mod1269
}
}
dp[index][useSteps] = ans
return ans
}
return dfs(0, steps)
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,32 @@ import (
func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
name string
steps, arrLen int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", 3, 2, 4},
{"TestCase2", 2, 4, 2},
{"TestCase3", 4, 2, 8},
{"TestCase4", 430, 148488, 525833932},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.steps, c.arrLen)
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.steps, c.arrLen)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}

0 comments on commit c40186b

Please sign in to comment.