-
Notifications
You must be signed in to change notification settings - Fork 185
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 #886 from 0xff-dev/552
Add solution and test-cases for problem 552
- Loading branch information
Showing
3 changed files
with
69 additions
and
34 deletions.
There are no files selected for viewing
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
40 changes: 38 additions & 2 deletions
40
leetcode/501-600/0552.Student-Attendance-Record-II/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,41 @@ | ||
package Solution | ||
|
||
func Solution(x bool) bool { | ||
return x | ||
const mod552 = 1000000007 | ||
|
||
func Solution(n int) int { | ||
// 直接dfs很明显,10101那个例子就过不去了,需要增加cache | ||
cache := make([][][]int, n+1) | ||
for i := 0; i <= n; i++ { | ||
cache[i] = make([][]int, 3) | ||
for j := 0; j < 3; j++ { | ||
cache[i][j] = make([]int, 4) | ||
for k := 0; k < 4; k++ { | ||
cache[i][j][k] = -1 | ||
} | ||
} | ||
} | ||
var dfs func(int, int, int) int | ||
dfs = func(nn, a, l int) int { | ||
// a<2&&l<3 | ||
if a >= 2 || l >= 3 { | ||
return 0 | ||
} | ||
if nn == 0 { | ||
//放满了, | ||
return 1 | ||
} | ||
if cache[nn][a][l] != -1 { | ||
return cache[nn][a][l] | ||
} | ||
ans := 0 | ||
// 选择一个P,下一个随便放 | ||
ans = (ans + dfs(nn-1, a, 0)) % mod552 | ||
// 放置一个A | ||
ans = (ans + dfs(nn-1, a+1, 0)) % mod552 | ||
// 放置一个L, L需要连续,所以放A和P的时候将L设置为0,这里是+1 | ||
ans = (ans + dfs(nn-1, a, l+1)) % mod552 | ||
cache[nn][a][l] = ans | ||
return ans | ||
} | ||
return dfs(n, 0, 0) | ||
} |
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