思路:

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的更多相关文章

  1. [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 ...

  2. 动态规划——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 ...

  3. 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 ...

  4. [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 ...

  5. [Algo] 66. All Valid Permutations Of Parentheses I

    Given N pairs of parentheses “()”, return a list with all the valid permutations. Assumptions N > ...

  6. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  7. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

  8. leetcode hard

    # Title Solution Acceptance Difficulty Frequency     4 Median of Two Sorted Arrays       27.2% Hard ...

  9. 【LeetCode】分治法 divide and conquer (共17题)

    链接:https://leetcode.com/tag/divide-and-conquer/ [4]Median of Two Sorted Arrays [23]Merge k Sorted Li ...

随机推荐

  1. 46、[源码]-Spring容器创建-注册BeanPostProcessors

    46.[源码]-Spring容器创建-注册BeanPostProcessors 6.registerBeanPostProcessors(beanFactory);注册BeanPostProcesso ...

  2. 使用webuploader实现大文件分片上传

    文件夹数据库处理逻辑 public class DbFolder { JSONObject root; public DbFolder() { this.root = new JSONObject() ...

  3. bootstrap中tab切换的使用

    文档地址:https://v3.bootcss.com/javascript/#tabs 简单实例: <!DOCTYPE html> <html lang="en" ...

  4. yum安装错误记录

    原因:使用yum安装libvirt以后,后续没有使用yum -remove 包名去移除这个包,接着使用源码安装了libvirt服务,当我卸载源码安装的libvirt以后,通过yum重新安装libvir ...

  5. Arts打卡第5周

    Algorithm.主要是为了编程训练和学习. 每周至少做一个 leetcode 的算法题(先从Easy开始,然后再Medium,最后才Hard). 进行编程训练,如果不训练你看再多的算法书,你依然不 ...

  6. x2goserver 连接问题

    The remote proxy closed the connection while negotiating the session. This may be due to the wrong a ...

  7. CSAW Quals CTF 2017-scv

    目录 程序基本信息 程序漏洞 整体思路 exp脚本 内容参考 程序基本信息 64位动态链接程序,开启了栈溢出和数据段不可执行保护 程序漏洞 read函数很明显的栈溢出漏洞 整体思路 由于题目给了lib ...

  8. Java 面向对象(六)

    抽象类和抽象方法 抽象方法 在方法前面添加了一个关键字 abstract 抽象方法的特点 (1)抽象方法是没有方法体的. (2)抽象方法必须得要定义在抽象类 或 接口当中 (在类前面添加上了一个abs ...

  9. MySQL 中视图和表的区别以及联系是什么?

    两者的区别: (1)视图是已经编译好的 SQL 语句,是基于 SQL 语句的结果集的可视化的表,而表不是. (2)视图没有实际的物理记录,而基本表有. (3)表是内容,视图是窗口. (4)表占用物理空 ...

  10. Webpack中的sourcemap以及如何在生产和开发环境中合理的设置

    一 . 从Sourcemap和Data URL说起 (1)什么是Sourcemap? 我们在打包中,将开发环境中源代码经过压缩,去空格,babel编译转化,最终可以得到适用于生产环境的项目代码,这样处 ...