-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #662 from 0xff-dev/1269
Add solution and test-cases for problem 1269
- Loading branch information
Showing
3 changed files
with
75 additions
and
26 deletions.
There are no files selected for viewing
36 changes: 23 additions & 13 deletions
36
...1-1300/1269.Number-of-Ways-to-Stay-in-the-Same-Place-After-Some-Steps/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 40 additions & 2 deletions
42
...code/1201-1300/1269.Number-of-Ways-to-Stay-in-the-Same-Place-After-Some-Steps/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters