Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit 0875232

Browse files
committed
903 unfinish
1 parent 5a02932 commit 0875232

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,32 @@
11
package problem0903
22

3+
const mod = 1e9 + 7
4+
5+
// ref: https://leetcode.com/problems/valid-permutations-for-di-sequence/discuss/168278/C++JavaPython-DP-Solution-O(N2)
36
func numPermsDISequence(S string) int {
7+
n := len(S)
8+
9+
dp := [201][201]int{}
10+
11+
for j := 0; j <= n; j++ {
12+
dp[0][j] = 1
13+
}
14+
15+
for i := 0; i < n; i++ {
16+
count := 0
17+
if S[i] == 'I' {
18+
for j := 0; j < n-i; j++ {
19+
count += dp[i][j]
20+
dp[i+1][j] = count % mod
21+
22+
}
23+
} else {
24+
for j := n - i - 1; j >= 0; j-- {
25+
count += dp[i][j+1]
26+
dp[i+1][j] = count % mod
27+
}
28+
}
29+
}
430

5-
return 0
31+
return dp[n][0]
632
}

Algorithms/0903.valid-permutations-for-di-sequence/valid-permutations-for-di-sequence_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ var tcs = []struct {
1818
5,
1919
},
2020

21+
{
22+
"DIDDDIDDDIDDDIDDDIDDDIDDDIDDDIDDDIDDDIDDDIDDDIDDDIDDDIDDDIDDDIDDDIDDDIDDDIDDDIDDDIDDDIDDDIDDDIDDDIDDDIDDDIDDDIDDDIDDDIDDDIDDDIDDDIDDDIDDDIDDDIDDDIDDDIDDDIDDDIDDDIDDDIDDDIDDDIDDDIDDDIDDDIDDDIDDDIDDDIDD",
23+
795732224,
24+
},
25+
2126
// 可以有多个 testcase
2227
}
2328

0 commit comments

Comments
 (0)