We are given S, a length n string of characters from the set {'D', 'I'}. (These letters stand for "decreasing" and "increasing".)

valid permutation is a permutation P[0], P[1], ..., P[n] of integers {0, 1, ..., n}, such that for all i:

  • If S[i] == 'D', then P[i] > P[i+1], and;
  • If S[i] == 'I', then P[i] < P[i+1].

How many valid permutations are there?  Since the answer may be large, return your answer modulo 10^9 + 7.

Example 1:

Input: "DID"
Output: 5
Explanation:
The 5 valid permutations of (0, 1, 2, 3) are:
(1, 0, 3, 2)
(2, 0, 3, 1)
(2, 1, 3, 0)
(3, 0, 2, 1)
(3, 1, 2, 0)

Note:

  1. 1 <= S.length <= 200
  2. S consists only of characters from the set {'D', 'I'}.

Approach #1: DP.[C++]

class Solution {
public:
int numPermsDISequence(string S) {
int n = S.length(), mod = 1e9 + 7;
vector<vector<int>> dp(n+1, vector<int>(n+1));
for (int j = 0; j <= n; ++j) dp[0][j] = 1;
for (int i = 0; i < n; ++i) {
if (S[i] == 'I') {
for (int j = 0, cur = 0; j < n - i; ++j)
dp[i+1][j] = cur = (cur + dp[i][j]) % mod;
} else {
for (int j = n - i - 1, cur = 0; j >= 0; --j)
dp[i+1][j] = cur = (cur + dp[i][j+1]) % mod;
}
}
return dp[n][0];
}
};

  

Analysis:

I feel this code is right, but I can't express why.

https://leetcode.com/problems/valid-permutations-for-di-sequence/discuss/168278/C%2B%2BJavaPython-DP-Solution-O(N2)

903. Valid Permutations for DI Sequence的更多相关文章

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

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

  3. 动态规划——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. leetcode903 Valid Permutations for DI Sequence

    思路: dp[i][j]表示到第i + 1个位置为止,并且以剩下的所有数字中第j + 1小的数字为结尾所有的合法序列数. 实现: class Solution { public: int numPer ...

  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. All LeetCode Questions List 题目汇总

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

  7. leetcode hard

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

  8. Swift LeetCode 目录 | Catalog

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

  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. C#窗体中将窗体按钮与键盘关联

    当击了某个按钮相当于是按了某个键盘上按键 private void btnPre_Click(object sender, EventArgs e) {     this.treeView1.Focu ...

  2. Bug of VS2015+WDK

    1>  Signability test failed.1>  1>  Errors:1>  22.9.7: DriverVer set to incorrect date ( ...

  3. python gun readline

    https://github.com/ludwigschwardt/python-gnureadline

  4. drbd mysql

    使用drbd前 升级内核 yum install kernel* 重启 4.1.准备: 两个节点ha-node1和ha-node2均按照centos7.0系统,每个节点两块磁盘,一块用作根分区一块用作 ...

  5. web前端整套面试题(一)--js相关

    一.单选 1.以下哪条语句会产生运行:(A) A.var obj = ( ); B.var obj = [ ]; C.var obj = { }; D.var obj = / /; B代表数组,C代表 ...

  6. VUE,使用物理引擎Box2D设计类愤怒小鸟的击球游戏--基本架构设置

  7. Unity游戏语音(富文本消息)解决方案GVoice

    腾迅云-GVoice https://www.qcloud.com/document/product/556/7673 集成1-2天内可搞定,博主亲测 Unity5.4.4f1 今天又试了下语音转文字 ...

  8. 架构之路:nginx与IIS服务器搭建集群实现负载均衡

    http://blog.csdn.net/zhanghan18333611647/article/details/50811980

  9. 相机IMU融合四部曲(三):MSF详细解读与使用

    相机IMU融合四部曲(三):MSF详细解读与使用 极品巧克力 前言 通过前两篇文章,<D-LG-EKF详细解读>和<误差状态四元数详细解读>,已经把相机和IMU融合的理论全部都 ...

  10. Pygame:编写一个小游戏 标签: pythonpygame游戏 2017-06-20 15:06 103人阅读 评论(0)

    大学最后的考试终于结束了,迎来了暑假和大四的漫长的"自由"假期.当然要自己好好"玩玩"了. 我最近在学习Python,本意是在机器学习深度学习上使用Python ...