思路:

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. js中数组元素的添加和删除

    js中数组元素常用添加方法是直接添加.push方法以及unshift方法 删除方法则是delete.pop.shift 集修改方法为一身的则是splice 1.添加: (1)直接添加通常都是这样 va ...

  2. 38 | 都说InnoDB好,那还要不要使用Memory引擎?

    我在上一篇文章末尾留给你的问题是:两个 group by 语句都用了 order by null,为什么使用内存临时表得到的语句结果里,0 这个值在最后一行:而使用磁盘临时表得到的结果里,0 这个值在 ...

  3. learning scala Case Classses

    package com.aura.scala.day01 object caseClasses { def main(args: Array[String]): Unit = { // 注意在实例化案 ...

  4. 代码 | 自适应大邻域搜索系列之(7) - 局部搜索LocalSearch的代码解析

    前言 好了小伙伴们我们又见面了,咳咳没错还是我.不知道你萌接连被这么多篇代码文章刷屏是什么感受,不过,酸爽归酸爽.今天咱们依然讲代码哈~不过今天讲的依然很简单,关于局部搜索LocalSearch的代码 ...

  5. 我的公众号 - Old Artist

    如果可以,我希望大家关注我的公众号,没有技术,没有华丽的文笔,只有简简单单的语句. 分享一些小的事情,大家好,我是老艺术家. Thanks~

  6. python打包成exe,太大了该怎么解决?

    这是一个很长的故事,嫌长的直接看最后的结论 事情经过 上周接了个需求,写了个小工具给客户,他要求打包成exe文件,这当然不是什么难事.因为除了写Python的,绝大多数人电脑里都没有Python编译器 ...

  7. getLocation需要在app.json中声明permission字段,解决办法

    具体开发方法如下: 在 app.json 里面增加 permission 属性配置(小游戏需在game.json中配置): "permission": { "scope. ...

  8. StrictMode 详解

    StrictMode类是Android 2.3 (API 9)引入的一个工具类,可以用来帮助开发者发现代码中的一些不规范的问题.比如,如果你在UI线程中进行了网络或者磁盘操作,StrictMode就会 ...

  9. java maven scope compile,provide,system,test,runtime

    在一个maven项目中,如果存在编译需要而发布不需要的jar包,可以用scope标签,值设为provided.如下: <dependency>            <groupId ...

  10. MSP与PSP

    摘抄自Triton.zhang——eeworld 1. MSP和PSP 的含义是Main_Stack_Pointer 和Process_Stack_Pointer,在逻辑地址上他们都是R13 2. 权 ...