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 ...
随机推荐
- 肤浅的聊聊关联子查询,数据集连接,TiDB代码,关系代数,等等
本章涉及的内容是TiDB的计算层代码,就是我们编译完 TiDB 后在bin目录下生成的 tidb-server 的可执行文件,它是用 go 实现的,里面对 TiPD 和 TiKV实现了Mock,可以单 ...
- java利用webuploader实现超大文件分片上传、断点续传
我们平时经常做的是上传文件,上传文件夹与上传文件类似,但也有一些不同之处,这次做了上传文件夹就记录下以备后用. 首先我们需要了解的是上传文件三要素: 1.表单提交方式:post (get方式提交有大小 ...
- vue+上传文件夹
在做项目开发的时候,上传东西无论文件也好,还是文件夹都需要用到 <input type="file" id="filepicker" name=" ...
- string字符串类型用scanf读入,printf输出
#include <iostream> #include <stdio.h> #include <string.h> using namespace std; in ...
- 【luoguP4777】【模板】扩展中国剩余定理(EXCRT)
(扩展)中国剩余定理 对于一组同余方程 \(x\equiv a_1(mod \quad n_1)\) \(x\equiv a_2(mod \quad n_2)\) \(x\equiv a_3(mod ...
- Storage Port Drivers
为了学习存储知识,我也是拼了,来,翻译一下下面这篇微软的文章(如果有谁翻译过了,或者微软有中文翻译,请绕路): Storage Port Drivers Last Updated: 4/20/2017 ...
- dp之斜率优化
前几天想练练思维,所以从cf上随便找了一道dp题,看完题意后第一感觉很简单,就是简单的区间dp题,但是看到数据范围的我顿时就懵了,(1≤n≤105) emmmmmmmm,按照普通的思路肯定会超时的.. ...
- C#MD5方法
不同形式,一样结果 /// <summary> /// 获取大写的MD5签名结果 /// </summary> /// <param name="encypSt ...
- LayUI使用弹窗重载父级数据表格的两种方法
参考LayUI官方文档,在子窗口中重载父级数据表格找到以下两种方法: 1.子窗口中重载:在子窗口中直接调用父级talbe的reload方法. $("body").on(" ...
- java.lang.ClassNotFoundException:org.apache.struts2.dispatcher.FilterDispatcher
老版本的Struts2升级,启动报的错. org.apache.struts2.dispatcher.FilterDispatcher 是web.xml中对struts2 2.2版本的接入点的类. ...