leetcode 903. DI序列的有效排列
题目描述:
我们给出 S,一个源于 {'D', 'I'} 的长度为 n 的字符串 。(这些字母代表 “减少” 和 “增加”。)
有效排列 是对整数 {0, 1, ..., n} 的一个排列 P[0], P[1], ..., P[n],使得对所有的 i:
如果 S[i] == 'D',那么 P[i] > P[i+1],以及;
如果 S[i] == 'I',那么 P[i] < P[i+1]。
有多少个有效排列?因为答案可能很大,所以请返回你的答案模 10^9 + 7.
示例:
输入:"DID"
输出:5
解释:
(0, 1, 2, 3) 的五个有效排列是:
(1, 0, 3, 2)
(2, 0, 3, 1)
(2, 1, 3, 0)
(3, 0, 2, 1)
(3, 1, 2, 0)
提示:
1 <= S.length <= 200
S
仅由集合{'D', 'I'}
中的字符组成。
思路分析:
我们用 dp(i, j) 表示确定了排列中到 P[i] 为止的前 i + 1 个元素,并且 P[i] 和未选择元素的相对大小为 j 的方案数(即未选择的元素中,有 j 个元素比 P[i] 小)。在状态转移时,dp(i, j) 会从 dp(i - 1, k) 转移而来,其中 k 代表了 P[i - 1] 的相对大小。如果 S[i - 1] 为 D,那么 k 不比 j 小;如果 S[i - 1] 为 I,那么 k 必须比 j 小。
代码:
class Solution {
public:
int mod = 1e9+;
int numPermsDISequence(string S) {
int n = S.size();
if(n==)
return ;
vector<vector<int>> dp(n+, vector<int>(n+, ));
for(int i=; i<n+; i++)
dp[][i]=;
for(int i=; i<=n; i++)
{
for(int j=; j<=i; j++)
{
if(S[i-]=='D')
{
for(int k=j; k<i; k++)
{
dp[i][j] += dp[i-][k];
dp[i][j] %= mod;
}
}
else
{
for(int k=; k<j; k++)
{
dp[i][j] += dp[i-][k];
dp[i][j] %= mod;
}
}
}
}
int ans = ;
for(int i=; i<=n; i++)
{
ans += dp[n][i];
ans %= mod;
}
return ans;
}
};
leetcode 903. DI序列的有效排列的更多相关文章
- [LeetCode] 903. Valid Permutations for DI Sequence DI序列的有效排列
We are given S, a length n string of characters from the set {'D', 'I'}. (These letters stand for &q ...
- [Swift]LeetCode903. DI 序列的有效排列 | Valid Permutations for DI Sequence
We are given S, a length n string of characters from the set {'D', 'I'}. (These letters stand for &q ...
- [LeetCode] Permutation Sequence 序列排序
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Sequence Reconstruction 序列重建
Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. Th ...
- [leetcode](4.21)2. 按字典序排列最小的等效字符串
给出长度相同的两个字符串:A 和 B,其中 A[i] 和 B[i] 是一组等价字符.举个例子,如果 A = "abc" 且 B = "cde",那么就有 'a' ...
- Leetcode题库——31.下一个排列
@author: ZZQ @software: PyCharm @file: nextPermutation.py @time: 2018/11/12 15:32 要求: 实现获取下一个排列的函数,算 ...
- #leetcode刷题之路47-全排列 II
给定一个可包含重复数字的序列,返回所有不重复的全排列.示例:输入: [1,1,2]输出:[ [1,1,2], [1,2,1], [2,1,1]] 之前的https://www.cnblogs.com/ ...
- [LeetCode] 31. Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- LeetCode——376.摆动序列
如果连续数字之间的差严格地在正数和负数之间交替,则数字序列称为摆动序列.第一个差(如果存在的话)可能是正数或负数.少于两个元素的序列也是摆动序列. 例如, [1,7,4,9,2,5] 是一个摆动序列, ...
随机推荐
- sentry 9.1.1docker版onepremise过程记录
sentry安装:https://github.com/getsentry/onpremise正确使用此文档安装步骤,安装版本9.1.1-onbuild,这个需要自改Dockerfile. 备注:构建 ...
- 搭建rsyslog日志服务器
环境配置 centos7系统 client1:192.168.91.17 centos7系统 master:192.168.91.18 rsyslog客户端配置 1.rsyslog安装 yum ins ...
- docker安装kafka
文本摘自此文章 .kafka需要zookeeper管理,所以需要先安装zookeeper. 下载zookeeper镜像 $ docker pull wurstmeister/zookeeper .启动 ...
- Log4基本配置
前言:作为一个程序员你要学会调试,对于一种调试都无法找到问题所在的情况,你要学会看日志,要学会看日志你的学会怎么样去写入日志,接下来教你配置C#Log4 第一步,你的在配置文件中配置好对应的参数 &l ...
- python3模块
一.sys模块 import sys #print(sys.path) #打印环境变量 #print(sys.argv) print(sys.argv[3]) Sys.argv[ ]其实就是一个列表, ...
- DevExpress的TreeList的常用属性设置以及常用事件
场景 Winform控件-DevExpress18下载安装注册以及在VS中使用: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/1 ...
- CSS animation属性
定义和用法 animation属性是下列属性的一个缩写属性: animation-name animation-duration animation-timing-function animation ...
- asp.net core 系列 2 启动类 Startup.CS
学无止境,精益求精 十年河东,十年河西,莫欺少年穷 学历代表你的过去,能力代表你的现在,学习代表你的将来 在探讨Startup启动类之前,我们先来了解下Asp.NET CORE 配置应用程序的执行顺序 ...
- 单点登录,系统B如何辨别用户已登录系统A
首先系统A去访问受限资源,跳转到sso认证中心https://login.sso.com/login?redirectURL=https://www.a.com/center,用户登录成功之后,sso ...
- 201871010101-陈来弟《面向对象程序设计(java)》第十五周学习总结
...