leetcode903 Valid Permutations for DI Sequence
思路:
dp[i][j]表示到第i + 1个位置为止,并且以剩下的所有数字中第j + 1小的数字为结尾所有的合法序列数。
实现:
class Solution
{
public:
int numPermsDISequence(string S)
{
if (S.empty()) return ;
int n = S.length(), MOD = 1e9 + ;
vector<vector<int>> dp(, vector<int>(n + , ));
for (int i = ; i <= n; i++) dp[][i] = ;
for (int i = ; i <= n; i++)
{
if (S[i - ] == 'D')
{
dp[i & ][n - i] = dp[i - & ][n - i + ];
for (int j = n - i - ; j >= ; j--)
dp[i & ][j] = (dp[i & ][j + ] + dp[i - & ][j + ]) % MOD;
}
else
{
dp[i & ][] = dp[i - & ][];
for (int j = ; j <= n - i; j++)
dp[i & ][j] = (dp[i - & ][j] + dp[i & ][j - ]) % MOD;
}
}
return dp[n & ][];
}
}
leetcode903 Valid Permutations for DI Sequence的更多相关文章
- [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 ...
- 动态规划——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 ...
- 903. 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] 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 ...
- [Algo] 66. All Valid Permutations Of Parentheses I
Given N pairs of parentheses “()”, return a list with all the valid permutations. Assumptions N > ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
- leetcode hard
# Title Solution Acceptance Difficulty Frequency 4 Median of Two Sorted Arrays 27.2% Hard ...
- 【LeetCode】分治法 divide and conquer (共17题)
链接:https://leetcode.com/tag/divide-and-conquer/ [4]Median of Two Sorted Arrays [23]Merge k Sorted Li ...
随机推荐
- 转,sql server update set from inner 批量修改的使用
SQL update select结合语句详解及应用 QL update select语句 最常用的update语法是: 1 2 UPDATE TABLE_NAME SET column_name ...
- Mysql查看所有表的数据量
##查看所有表信息 SELECT * FROM information_schema.TABLES WHERE TABLE_SCHEMA = 'pcms-zgh20190327' ##查看各个表数据量 ...
- div与div之间有空隙
当你使用HTML div块与块的中间不能紧密连接 怎么都解决不了时 使用前效果图 可以在<head></head>中间内容里加一个 * { margin:0; padding ...
- Linux ntp 时间同步服务配置
一.基础环境 1.操作系统:CentOS 7.3 2.ntp:4.2.6 3.机器,服务端(192.168.1.210)客户端(192.168.1.211) 二.安装ntp yum -y instal ...
- span标签
<span>在行内定义一个区域,也就是一行内可以被<span>划分成好几个区域,从而实现某种特定效果.<span>本身没有任何属性. <span> 与& ...
- Oracle Linux 6.4 LVM中误删VG之恢复过程
一.项目背景描述 1.OSS现网测试数据库因大量小事物频繁提交运行非常缓慢.经分析为DS3950存储所在磁盘I/O存在瓶颈,大量等待事件,性能受限.另外,开发同事没有优化意识,没将小事物做成批量提交方 ...
- Codeforces Round #528 (Div. 2)题解
Codeforces Round #528 (Div. 2)题解 A. Right-Left Cipher 很明显这道题按题意逆序解码即可 Code: # include <bits/stdc+ ...
- 使用Ajax和一般处理程序实现文件上传与下载
1.使用HTML的input标签 <input type="file" multiple="multiple" id="file_load&qu ...
- CSPS2019游(tuifei)记
%%%脸哥没脸%%% Day0,日常考前紧张,做不下题去.听各大主任送祝福(从里红(wa)到外) 然后就出发了,大巴上和云力一起坐,吃了好多东西.中午因不满火车站的不合理收费,选择了面包+火腿 下午在 ...
- JavaBean转Map
1.需要的jar包 <dependency> <groupId>com.google.guava</groupId> <artifactId>guava ...