-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution903.cs
44 lines (39 loc) · 1.06 KB
/
Solution903.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using LeetCode.DataStructures;
namespace LeetCode.Solutions;
public class Solution903
{
/// <summary>
/// 903. Valid Permutations for DI Sequence - Hard
/// <a href="https://leetcode.com/problems/valid-permutations-for-di-sequence">See the problem</a>
/// </summary>
public int NumPermsDISequence(string s)
{
int n = s.Length;
int mod = 1000000007;
var dp = new int[n + 1, n + 1];
for (int i = 0; i <= n; i++)
{
dp[0, i] = 1;
}
for (int i = 0; i < n; i++)
{
if (s[i] == 'I')
{
for (int j = 0, cur = 0; j < n - i; j++)
{
cur = (cur + dp[i, j]) % mod;
dp[i + 1, j] = cur;
}
}
else
{
for (int j = n - i - 1, cur = 0; j >= 0; j--)
{
cur = (cur + dp[i, j + 1]) % mod;
dp[i + 1, j] = cur;
}
}
}
return dp[n, 0];
}
}