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

    https://github.com/zhuayi/xbuild

  2. NavigationController.viewControllers

      NSMutableArray *viewControllersArray = [NSMutableArray new];    // 获取当前控制器数组    for (CardLoanBaseT ...

  3. 一款比较好用的JS时间控件-laydate

      官方讲解:http://laydate.layui.com/ 具体的内容请看官方讲解,此处仅说名应用: 1.在jsp或html或其他中引入laydate.js <script src=&qu ...

  4. HEIDSOFT

    HEIDSOFT ENTHUSIASTIC GITHUB USER heidsoft@sina.com GitHub Profile I'm a developer based in China.sh ...

  5. 开启Windows8里面的Hyper-V虚拟机功能

    首先了解下什么是Hyper-V?也就是虚拟化技术,允许终端用户在同一台机器上运行多个操作系统,支持32位和64位系统,可以直接在Windows 8上创建自己的虚拟机.开启Hyper-V虚拟机需要更多的 ...

  6. Redis学习(1)——下载与配置[转]

    Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.从2010年3月15日起,Redis的开发工作由VMware主 ...

  7. 洛谷 P3659 [USACO17FEB]Why Did the Cow Cross the Road I G

    //神题目(题目一开始就理解错了)... 题目描述 Why did the cow cross the road? Well, one reason is that Farmer John's far ...

  8. tp5写日志

  9. 在git bash中使用命令行调用tortoisegit提交代码或查看日志

    Tortoisegit commit / show log命令行 TortoiseGitProc.exe /command:commit TortoiseGitProc.exe /command:lo ...

  10. Linux Mint 17使用配置

    => 调亮度: 方法一: 每次开机或注销登录之后需要重新设置 > /sys/class/backlight/intel_backlight/brightness #根据自己需要调值,如20 ...